Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload an image from Android to Amazon S3?

I need to upload a bitmap to Amazon S3. I have never used S3, and the docs are proving less than helpful as I can't see anything to cover this specific requirement. Unfortunately I'm struggling to find time on this project to spend a whole day learning how it all hangs together so hoping one of you kind people can give me some pointers.

Can you point to me to a source of reference that explains how to push a file to S3, and get a URL reference in return?

More specifically: - Where do the credentials go when using the S3 Android SDK? - Do I need to create a bucket before uploading a file, or can they exist outside buckets? - Which SDK method do I use to push a bitmap up to S3? - Am I right in thinking I need the CORE and S3 libs to do what I need, and no others?

like image 232
Ollie C Avatar asked Sep 05 '11 15:09

Ollie C


People also ask

How do I upload images to Amazon S3?

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.

Can we store images in S3?

You can create a dataset using images stored in an Amazon S3 bucket. With this option, you can use the folder structure in your Amazon S3 bucket to automatically label your images. You can store the images in the console bucket or another Amazon S3 bucket in your account.


3 Answers

String      ACCESS_KEY="****************",
            SECRET_KEY="****************",
            MY_BUCKET="bucket_name",
            OBJECT_KEY="unique_id";              
  AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
                AmazonS3 s3 = new AmazonS3Client(credentials);
                java.security.Security.setProperty("networkaddress.cache.ttl" , "60");
                s3.setRegion(Region.getRegion(Regions.AP_SOUTHEAST_1));
                s3.setEndpoint("https://s3-ap-southeast-1.amazonaws.com/");
                List<Bucket> buckets=s3.listBuckets();
                for(Bucket bucket:buckets){
                    Log.e("Bucket ","Name "+bucket.getName()+" Owner "+bucket.getOwner()+ " Date " + bucket.getCreationDate());
                }
                Log.e("Size ", "" + s3.listBuckets().size());
                TransferUtility transferUtility = new TransferUtility(s3, getApplicationContext());
                UPLOADING_IMAGE=new File(Environment.getExternalStorageDirectory().getPath()+"/Screenshot.png");
                TransferObserver observer = transferUtility.upload(MY_BUCKET,OBJECT_KEY,UPLOADING_IMAGE);
                observer.setTransferListener(new TransferListener() {
                    @Override
                    public void onStateChanged(int id, TransferState state) {
                        // do something
                        progress.hide();
                        path.setText("ID "+id+"\nState "+state.name()+"\nImage ID "+OBJECT_KEY);

                    }

                    @Override
                    public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                        int percentage = (int) (bytesCurrent / bytesTotal * 100);
                        progress.setProgress(percentage);
                        //Display percentage transfered to user
                    }

                    @Override
                    public void onError(int id, Exception ex) {
                        // do something
                        Log.e("Error  ",""+ex );
                    }

                });
like image 133
Andan H M Avatar answered Oct 11 '22 03:10

Andan H M


Take a look at the Amazon S3 API documentation to get a feel for what can and can't be done with Amazon S3. Note that there are two APIs, a simpler REST API and a more-involved SOAP API.

You can write your own code to make HTTP requests to interact with the REST API, or use a SOAP library to consume the SOAP API. All of the Amazon services have these standard API endpoints (REST, SOAP) and in theory you can write a client in any programming language!

Fortunately for Android developers, Amazon have released a (Beta) SDK that does all of this work for you. There's a Getting Started guide and Javadocs too. With this SDK you should be able to integrate S3 with your application in a matter of hours.

The Getting Started guide comes with a full sample and shows how to supply the required credentials.

Conceptually, Amazon S3 stores data in Buckets where a bucket contains Objects. Generally you'll use one bucket per application, and add as many objects as you like. S3 doesn't support or have any concept of folders, but you can put slashes (/) in your object names.

like image 37
David Snabel-Caunt Avatar answered Oct 11 '22 02:10

David Snabel-Caunt


We can directly use "Amazone s3" bucket for storing any type of file on server, and we did not need to send any of file to Api server it will reduce the request time.

Gradle File :-

 compile 'com.amazonaws:aws-android-sdk-core:2.2.+'
    compile 'com.amazonaws:aws-android-sdk-s3:2.2.+'
    compile 'com.amazonaws:aws-android-sdk-ddb:2.2.+'

Manifest File :-

<service android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
            android:enabled="true" />

FileUploader Function in any Class :-

 private void setUPAmazon() {
 //we Need Identity Pool ID  like :- "us-east-1:f224****************8"
        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(getActivity(),
                "us-east-1:f224****************8", Regions.US_EAST_1);
        AmazonS3 s3 = new AmazonS3Client(credentialsProvider);
        final TransferUtility transferUtility = new TransferUtility(s3, getActivity());
        final File file = new File(mCameraUri.getPath());
        final TransferObserver observer = transferUtility.upload(GeneralValues.AMAZON_BUCKET, file.getName(), file, CannedAccessControlList.PublicRead);
        observer.setTransferListener(new TransferListener() {
            @Override
            public void onStateChanged(int id, TransferState state) {
                Log.e("onStateChanged", id + state.name());
                if (state == TransferState.COMPLETED) {
                    String url = "https://"+GeneralValues.AMAZON_BUCKET+".s3.amazonaws.com/" + observer.getKey();
                    Log.e("URL :,", url);
//we just need to share this File url with Api service request.  
                }
            }

            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
            }

            @Override
            public void onError(int id, Exception ex) {
                Toast.makeText(getActivity(), "Unable to Upload", Toast.LENGTH_SHORT).show();
                ex.printStackTrace();
            }
        });
    }
like image 20
A-Droid Tech Avatar answered Oct 11 '22 03:10

A-Droid Tech