Since it seems we are limited to the number of buckets, I'm trying to figure out how to accomplish the following:
Am I missing something in the documentation? I appreciate any feedback on this issue.
jpg . Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.
The AWSMobileClient provides client APIs and building blocks for developers who want to create user authentication experiences.
To solve this problem, I started with Amazon's sample code in their iOS SDK, found here. In the SDK zip, the sample project of interest can be found at samples/S3_Uploader
.
To get from that sample project to one in which the uploaded image is public, you simply need to add one line in the right place:
por.cannedACL = [S3CannedACL publicRead];
where por
is the S3PutObjectRequest
used to upload the image.
My project's code for uploading looks like this (looks almost identical to Amazon's sample code):
NSString *uuid = @""; // Generate a UUID however you like, or use something else to name your image.
UIImage *image; // This is the UIImage you'd like to upload.
// This URL is not used in the example, but it points to the file
// to be uploaded.
NSString *url = [NSString pathWithComponents:@[ @"https://s3.amazonaws.com/", AWS_PICTURE_BUCKET, uuid ]];
// Convert the image to JPEG data. Use UIImagePNGRepresentation for pngs
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
// Create the S3 Client.
AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:AWS_ACCESS_KEY_ID withSecretKey:AWS_SECRET_KEY];
@try {
// Create the picture bucket.
[s3 createBucket:[[S3CreateBucketRequest alloc] initWithName:AWS_PICTURE_BUCKET]];
// Upload image data. Remember to set the content type.
S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:uuid inBucket:AWS_PICTURE_BUCKET];
por.contentType = @"image/jpeg"; // use "image/png" here if you are uploading a png
por.cannedACL = [S3CannedACL publicRead];
por.data = imageData;
por.delegate = self; // Don't need this line if you don't care about hearing a response.
// Put the image data into the specified s3 bucket and object.
[s3 putObject:por];
}
@catch (AmazonClientException *exception) {
NSLog(@"exception");
}
AWS_ACCESS_KEY_ID
and AWS_SECRET_KEY
are, of course, your AWS credentials, and AWS_PICTURE_BUCKET
is your picture bucket.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With