Mobile Photo Uploads to Amazon S3
Mobile Photo Uploads to Amazon S3 using the AWS SDK for iOS and AWS SDK for Android
Submitted By: Glenn@
AWS Products Used: Amazon S3
Language(s): Objective-C, Java
Created On: October 13, 2011
Version 2 of the AWS Mobile SDK
- This article and sample apply to Version 1 of the AWS Mobile SDK. If you are building new apps, we recommend you use Version 2. For details, please visit the AWS Mobile SDK page.
- This content is being maintained for historical reference.
Mobile Photo Uploads to Amazon S3 using the AWS SDK for iOS and AWS SDK for Android.
This article demonstrates how to upload an image to Amazon S3 from your mobile device and how to make that image available on the web. Amazon S3 is storage for the Internet. It's a simple storage service that offers software developers a highly-scalable, reliable, secure, fast, and inexpensive data storage. The article shows sample code for both the iOS and Android platforms. The complete sample code and project files are included in the mobile SDKs for these platforms. Links to the SDKs are available at the end of this article.
To use the AWS SDK for iOS or the AWS SDK for Android, you will need your AWS credentials, that is, your Access Key ID and Secret Access Key. If you haven't already signed up for Amazon Web Services (AWS), you will need to do that first to get your AWS credentials. You can sign up for AWS here.
Here's what the sample app looks like at start up on iOS and Android:
- iOS
- Android
Image Upload
The app uses each platform's "image picker" utility to have the end-user select an image for upload. The app then creates an Amazon S3 client, uses the client to create an Amazon S3 bucket in which to store the image, and finally uploads the image into the bucket. A bucket is a container for objects stored in Amazon S3. Every object--such as an image--is contained within a bucket.
Get the image
The first step is to retrieve the content, in this case an image, to be uploaded to Amazon S3. For this sample app, selecting an image from the device itself is an easy choice.
- iOS
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; [self presentModalViewController:imagePicker animated:YES]; |
- Android
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, PHOTO_SELECTED); |
On both platforms, once an image is selected, a callback method is invoked with the selected image's information. The app uses this information to complete the upload.
Upload the image
Once we have the image, we can attempt to upload it to Amazon S3.
First, create an Amazon S3 client to communicate with the service.
- iOS
AmazonS3Client *s3 = [[[AmazonS3Client alloc] initWithAccessKey:MY_ACCESS_KEY_ID withSecretKey:MY_SECRET_KEY] autorelease]; |
- Android
AmazonS3Client s3Client = new AmazonS3Client( new BasicAWSCredentials( MY_ACCESS_KEY_ID, MY_SECRET_KEY ) ); |
Second, create an S3 bucket to store the picture.
- iOS
[s3 createBucket:[[[S3CreateBucketRequest alloc] initWithName:MY_PICTURE_BUCKET] autorelease]]; |
- Android
s3Client.createBucket( MY_PICTURE_BUCKET ); |
Finally, put the image object into the S3 bucket.
- iOS
S3PutObjectRequest *por = [[[S3PutObjectRequest alloc] initWithKey:MY_PICTURE_NAME inBucket:MY_PICTURE_BUCKET] autorelease]; por.contentType = @"image/jpeg"; por.data = imageData; [s3 putObject:por]; |
- Android
PutObjectRequest por = new PutObjectRequest( Constants.getPictureBucket(), Constants.PICTURE_NAME, new java.io.File( filePath) ); s3Client.putObject( por ); |
Browser Display
The app makes the image available for viewing in a browser by generating a pre-signed URL. A pre-signed URL is a URL for an Amazon S3 resource that is signed with current AWS security credentials. The pre-signed URL can then be shared with other users, allowing them to access resources without providing an account's AWS security credentials.
First, create an override content type to ensure that the "content" will be treated as an image by the browser.
- iOS
S3ResponseHeaderOverrides *override = [[[S3ResponseHeaderOverrides alloc] init] autorelease]; override.contentType = @"image/jpeg"; |
- Android
ResponseHeaderOverrides override = new ResponseHeaderOverrides(); override.setContentType( "image/jpeg" ); |
Second, create the pre-signed URL request. Pre-signed URLs can be created with an expiration date, that is, a date and time after which the resource will no longer be available. In the sample, the pre-signed URLs are valid for only one hour.
- iOS
S3GetPreSignedURLRequest *gpsur = [[[S3GetPreSignedURLRequest alloc] init] autorelease]; gpsur.key = PICTURE_NAME; gpsur.bucket = [Constants pictureBucket]; gpsur.expires = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval) 3600]; // Added an hour's worth of seconds to the current time. gpsur.responseHeaderOverrides = override; |
- Android
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest( Constants.getPictureBucket(), Constants.PICTURE_NAME ); urlRequest.setExpiration( new Date( System.currentTimeMillis() + 3600000 ) ); // Added an hour's worth of milliseconds to the current time. urlRequest.setResponseHeaders( override ); |
Third, generate the pre-signed URL.
- iOS
NSURL *url = [s3 getPreSignedURL:gpsur]; |
- Android
URL url = s3Client.generatePresignedUrl( urlRequest ); |
Finally, launch the browser to view the pre-signed URL which will display the image.
[[UIApplication sharedApplication] openURL:url]; |
startActivity( new Intent( Intent.ACTION_VIEW, Uri.parse( url.toURI().toString() ) ) ); |
Next Steps
These few lines of code demonstrate how Amazon S3 could become a limitless storage device for your mobile photos. A photo sharing app that allows users to view photos from other users would not be a difficult extension to the above code. Also, the content that is uploaded and shared is not limited to images. The content could be audio files, video files, text, or other content that users want to store and share.
References
A sample app that includes this code is hosted in our samples repositories on GitHub:
For more information about using AWS credentials with mobile applications see the following article:
Want to learn more?
To learn about mobile development best practices, follow our AWS Mobile Development Blog. You can also ask questions or post comments in the Mobile Development Forum about this or any other topic related to mobile development with AWS.