简体   繁体   English

使用改造将图像上传到Amazon S3

[英]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. 我无法解决如何将图像从Android上传到Amazon S3的麻烦。 I've been given an API from which I can get a signedRequest and a URL. 我获得了一个API,从中可以获取一个SignedRequest和一个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. 我正在使用翻新来获取来自API的响应,并且得到了signedRequest和URL很好,只是不知道如何继续使用它们从设备的图库中上传图片。

Here's the respone I got from the API (edited out personal info): 这是我从API获得的回复(已删除的个人信息):

{"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? 我应该如何继续使用从API获取的数据来将图像从设备的图库上传到S3服务?

1. 1。

If any signed request with some ids or keys are there then put url plus key to send response. 如果有带有某些ID或密钥的已签名请求,则将url加密钥发送响应。 First get the response 200 and you can try the below steps to send image 首先获得响应200,您可以尝试以下步骤发送图像

  1. Use Image Picker to get the gallery. 使用图像选择器获取图库。
  2. get the image path on activity result and save that path in String. 获取活动结果的图像路径,并将该路径保存在String中。
  3. Create a File for example File file = new File(your image string name).getabsolutepath(); 创建一个文件,例如File file = new File(您的图像字符串名称).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. 注意:在状态码上,如果获取响应200,则使用requestbody从multipart或Hashmap从文件创建part。 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 2.如果您没有任何后端,请检查此链接亚马逊aws上传

3. the easiest one ... //register in your manifest file inside application 3.最简单的一个……//在应用程序内部的清单文件中注册

<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 // build.gradle中的依赖

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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