简体   繁体   中英

Uploading an image to amazon s3 using retrofit

I'm having trouble wrapping my head around how to upload an image to Amazon S3 from Android. I've been given an API from which I can get a signedRequest and a URL. I'm supposed to use those to upload the image. I'm using retrofit to get the response from the API and I get the signedRequest and URL just fine, just don't know how to proceed to use them to upload an image from the device's gallery.

Here's the respone I got from the API (edited out personal info):

{"signedRequest": "https://test-name.s3.amazonaws.com/59a7ee45j2407504349ef4?AWSAccessKeyId=EKJJOHFINVRB2VL7LQ&Content-Type=image&Expires=1504177793&Signature=43ajtX%90841haoA8VsFe6xC57hedQ%3D&x-amz-acl=public-read",
"url": "https://test-name.s3.amazonaws.com/59a7ee45j2407504349ef4"}

Here's my retrofit interface for getting the above data:

    @GET("/sign-s3")
    Call<S3Sign> getSignedUrl(
        @Query("token") String token,
        @Query("file-type") String fileType);

and finally here's my code from activity:

    Button signed = (Button)findViewById(R.id.signedBtn);
    signed.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ApiInterface service = RestClient.getClient();
            Call<S3Sign> call = service.getSignedUrl(token, fileType);
            call.enqueue(new Callback<S3Sign>() {
                @Override
                public void onResponse(Call<S3Sign> call, Response<S3Sign> response) {
                    int statusCode = response.code();
                    S3Sign s3Sign = response.body();

                    if(statusCode == 200){
                        Log.d("SignedRequest: ", s3Sign.getSignedRequest());
                        Log.d("URL: ", s3Sign.getUrl());
                    }
                    else {
                        Toast.makeText(TestActivity.this, "Error", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<S3Sign> call, Throwable t) {
                    Log.d("Failure", t.getMessage());
                }
            });
        }
    });

How should I proceed to use the data which I got from the API to upload an image to S3 service from my device's gallery?

1.

If any signed request with some ids or keys are there then put url plus key to send response. First get the response 200 and you can try the below steps to send image

  1. Use Image Picker to get the gallery.
  2. get the image path on activity result and save that path in String.
  3. Create a File for example File file = new File(your image string name).getabsolutepath();
  4. pass this file to the retrofit in call however your data is been passed.

Note : On status code if getting response 200 then createpart from file from multipart or Hashmap using requestbody. if their is any parameters to get the image to be passed then add that parameters too. and your image is been submitted to the server.

2. If you are not having any backend then please check this link amazon aws upload

3. the easiest one ... //register in your manifest file inside application

<service
        android:name="com.onecode.s3.service.S3UploadService"
        android:exported="false" />

S3Credentials s3Credentials = new S3Credentials(accessKey, secretKey, sessionToken);
    S3BucketData s3BucketData = new S3BucketData.Builder()
            .setCredentials(s3Credentials)
            .setBucket(bucket)
            .setKey(key)
            .setRegion(region)
            .build();

S3UploadService.upload(getActivity(), s3BucketData, imagefile to upload, null);

// dependency in build.gradle

 compile 'com.github.OneCodeLabs:S3UploadService:1.0.0@aar'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM