Upload an image from Android to Amazon S3?

前端 未结 7 2062
不知归路
不知归路 2021-02-01 06:42

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. Unfortunate

7条回答
  •  遥遥无期
    2021-02-01 07:39

    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 :-

    
    

    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();
                }
            });
        }
    

提交回复
热议问题