Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I upload to AWS S3 from iOS without Cognito?

My Goal:

Upload photos from iOS to a common AWS S3 bucket with the simplest code possible


What I've Tried:

  • Reading the aws-sdk-ios repo
  • Reading the S3 Upload Files page
  • Reading the Cognito FAQ page
  • Reading various blogs and SO questions
  • Creating a Cognito pool with a client app

Assumptions of Best Practices:

I assume that Cognito is the default way to set up image uploading after all of the reading I've done. However, I have two problems with doing it this way:

  1. It seems overly complicated for something where I should just need to pass the image, bucket, key, and secret.
  2. I have already done this through a server before with just key and secret, so I don't see a reason to use or pay for Cognito.

My Questions:

  • Is there a way to upload from iOS to AWS S3 without using Cognito?
  • If any of my above assumptions or questions are not the best way to realize my goal, then what is the simplest way to do so?
like image 378
smileham Avatar asked Dec 18 '22 16:12

smileham


2 Answers

Cognito Identity is a free service so you don't really have to pay for it. Also you really shouldn't be using hardcoded credentials in your app since it can be easily decompiled and retrieved which is not the scenario in case of a server side code.

For iOS SDK you can use AWSStaticCredentialsProvider as a way to provide the static credentials to any service client.

AWSStaticCredentialsProvider *credentialsProvider = [AWSStaticCredentialsProvider credentialsWithAccessKey:@"YourAccessKey" secretKey:@"YourSecretKey"];

Again you really shouldn't be doing this in a production app.

like image 175
Karthik Avatar answered Jan 05 '23 01:01

Karthik


I suggest that never use AWS in Moblie Application without Cognito.

If you still want to do that you can create an IAM user set policy to your bucket resources and put the IAM user credential to your code

AWSCredentials credentials = new AWSCredentials() {     
        @Override
        public String getAWSSecretKey() {
            // TODO Auto-generated method stub
            return "YOUR_SECRETKEY";
        }
        
        @Override
        public String getAWSAccessKeyId() {
            // TODO Auto-generated method stub
            return "YOUR_IAM_AWS_ACCESS_KEY_ID";
        }
    };

    AmazonS3 s3 = new AmazonS3Client(credentials);

Note that the method is dangerous to use!! You can set AWS S3 Bucket permission policy instead: S3 permission policy.

like image 29
Cenxui Avatar answered Jan 05 '23 00:01

Cenxui