简体   繁体   English

PUT 上传文件到 AWS S3 预签名 url Retrofit2 Android

[英]PUT upload file to AWS S3 presigned url Retrofit2 Android

I have a pre-signed upload URL from AWS S3 to upload a video file to.我有一个来自 AWS S3 的预签名上传 URL,可以将视频文件上传到。 Testing on Postman the video is successfully uploaded.在 Postman 上测试视频上传成功。 However i get a 403 returned when implementing in retrofit.但是,在改造中实施时,我得到了 403 返回。 I cannot use multipart upload for this task.我无法为此任务使用分段上传。

service call :服务电话:

@Headers("Content-Type: video/mp4")
    @PUT
    fun uploadTaskAWS(@Url awsUrl: String, @Body filePart: RequestBody): Call<ResponseBody>

upload:上传:

            val file = File(task.file_path)
            val requestFile = RequestBody.create(MediaType.parse("video/mp4"), file)
            val response = awsTaskUploadService.uploadTaskAWS(task.upload_url, requestFile)

I have ensured the URL is correct.我已经确保 URL 是正确的。 The content type header is added too, postman screenshot attached.内容类型标题也被添加,附上邮递员截图。 Getting 403 Forbidden Error获取 403 禁止错误

screenshot of postman邮递员截图

I have found this related question however i'm still getting a 403. Upload a file to AWS S3 pre-signed URL using Retrofit2我发现了这个相关的问题,但是我仍然收到 403。 使用 Retrofit2 将文件上传到 AWS S3 预签名 URL

Any suggestions?有什么建议?

solution was to include multipart and expect Single response not Call :解决方案是包含 multipart 并期望 Single response 而不是 Call :

  @Multipart
    @PUT
    fun uploadAsset(
        @Header(CONTENT_TYPE) contentType: String,
        @Url uploadUrl: String,
        @Part file: MultipartBody.Part
    ): Single<ResponseBody>

where contentType is passed in传入 contentType 的地方

val requestFile = RequestBody.create(MediaType.parse(contentType), file)
val body = MultipartBody.Part.createFormData(mediaType, task.file_name, requestFile)
assetService.uploadAsset(contentType, task.upload_url, body)

403 error means the server knows who you are but do not have the authority to perform the desired action. 403 错误意味着服务器知道您是谁,但无权执行所需的操作。

If the pre-signed URL is correctly generated, you should be able to upload without any authentication.如果正确生成了预签名 URL,您应该无需任何身份验证即可上传。

You can check if postman is by default adding any AWS Authentication to request您可以检查邮递员是否默认添加任何 AWS 身份验证来请求

Also you need to explicitly set the protocol version while signing requests if uploading to a region that uses only version 4.如果上传到仅使用版本 4 的区域,您还需要在签署请求时明确设置协议版本。

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

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