This article demonstrates how to use the AWS SDK for Android to uploadan image to Amazon Simple StorageService (S3) from your mobile device and how to make that imageavailable on the web. Amazon S3 is storage for the Internet. It's a simplestorage service that offers software developers a highly-scalable,reliable, secure, fast, and inexpensive data storage. The complete samplecode and project files are included in the AWS SDK for Android which canbe found here.
To use the AWS SDK for Android, you will need your AWS credentials,that is, your Access Key ID and Secret Access Key. If you haven'talready signed up for Amazon WebServices (AWS), you will need to do that first to get your AWScredentials. You can sign up for AWS here.
Here's what the sample app looks like at start up on Android:
The app uses each platform's "image picker" utility to have theend-user select an image for upload. The app then creates an Amazon S3client, uses the client to create an Amazon S3 bucket in which to storethe image, and finally uploads the image into the bucket. A bucket is acontainer for objects stored in Amazon S3. Every object--such as animage--is contained within a bucket.
Get the image
The first step is to retrieve the content, in this case an image, tobe uploaded to Amazon S3. For this sample app, selecting an image from thedevice itself is an easy choice.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.setType("image/*");startActivityForResult(intent, PHOTO_SELECTED);
Once an image is selected, a callback method is invoked with theselected image's information. The app uses this information to completethe 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 withthe service.
AmazonS3Client s3Client = new AmazonS3Client( new BasicAWSCredentials( MY_ACCESS_KEY_ID, MY_SECRET_KEY ) );
Second, create an Amazon S3 bucket to store thepicture.
s3Client.createBucket( MY_PICTURE_BUCKET );
Finally, put the image object into the Amazon S3bucket.
PutObjectRequest por = new PutObjectRequest( Constants.getPictureBucket(), Constants.PICTURE_NAME, new java.io.File( filePath) );s3Client.putObject( por );
The app makes the image available for viewing in a browser bygenerating a pre-signed URL. A pre-signed URL is a URL for an Amazon S3resource that is signed with current AWS security credentials. Thepre-signed URL can then be shared with other users, allowing them toaccess resources without providing an account's AWS securitycredentials.
First, create an override content type to ensure thatthe "content" will be treated as an image by the browser.
ResponseHeaderOverrides override = new ResponseHeaderOverrides();override.setContentType( "image/jpeg" );
Second, create the pre-signed URL request. Pre-signedURLs can be created with an expiration date, that is, a date andtime after which the resource will no longer be available. In the sample,the pre-signed URLs are valid for only one hour.
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest( Constants.getPictureBucket(), Constants.PICTURE_NAME );// Added an hour's worth of milliseconds to the current time.urlRequest.setExpiration( new Date( System.currentTimeMillis() + 3600000 ) );urlRequest.setResponseHeaders( override );
Third, generate the pre-signed URL.
URL url = s3Client.generatePresignedUrl( urlRequest );
Finally, launch the browser to view the pre-signed URLwhich will display the image.
startActivity( new Intent( Intent.ACTION_VIEW, Uri.parse( url.toURI().toString() ) ));
These few lines of code demonstrate how Amazon S3 could become alimitless storage device for your mobile photos. A photo sharing app thatallows users to view photos from other users would not be a difficultextension to the above code. Also, the content that is uploaded and sharedis not limited to images. The content could be audio files, video files,text, or other content that users want to store and share.
A sample app that includes this code is provided with both mobile SDKs.The download links can be found here: AWS SDK for Android.
For more information about using AWS credentials with mobileapplications see the following article: Authenticating Users of AWS Mobile Applications with a Token VendingMachine
Please feel free to ask questions or make comments in the MobileDevelopment Forum.
Portions of this page are reproduced from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.