改造 - 使用预签名 URL 将图像上传到 S3

Retrofit - Upload Image to S3 using presigned URL

我正在从 Lambda 获取预签名 URL,但不确定为什么当我向生成的 URL 发出 PUT 请求时,我的图像没有上传到 S3。

我完全遵循了这两个答案,但它仍然不适合我

有人可以指出我做错了什么吗?

public interface IUploadImages{
   @PUT
   Call<String> listRepos(@Url String url, @Body RequestBody image);
}

还有我上传图片的电话:

        //Already checked to make sure mImageUri is not null
        File file = new File(StringUtils.getPath(this, mImageUri));
        RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), file);

        RetrofitInterfaces.IUploadImages service = RetrofitClientInstance.getRetrofitInstance()
                .create(RetrofitInterfaces.IUploadImages.class);
        //Also checked to make sure mGeneraredUrl is also not null
        Call<String> call = service.listRepos(mGeneratedUrl, requestFile);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful()){
                    Log.d(TAG, "onResponse: Success?");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Log.d(TAG, "onFailure: Failed: " + t);
            }
        });         

我做到了public。调用没有失败,但在 onResponse 中我收到了 403 的响应代码。

终于弄明白了..您的预签名 url 函数需要具有与

相同的内容类型

RequestBody.create(MediaType.parse("image/jpeg"), file);

所以在我的参数中

var params = {
    Bucket: 'my-bucket-name',
    Key: 'test-key',
    Expires: signedUrlExpireSeconds,
    ContentType: "image/jpeg"
};

客户端和函数端的内容类型必须相同,否则会出现 403 错误...