gpt4 book ai didi

android - 如何使用 Amazon Web Services 获取上传成功状态?

转载 作者:行者123 更新时间:2023-12-04 02:13:54 29 4
gpt4 key购买 nike

我正在开发一个使用 Amazon Web Services (AWS) 存储文件的 Android 项目。我有这个:

AmazonS3Client s3Client = new AmazonS3Client( new BasicAWSCredentials(myId, myKey) );
s3Client.putObject( new PutObjectRequest(myBucketName, myObjectKey, myFile) );

它已被证明是成功的,因为我可以看到亚马逊数据库中的文件。我想知道的是我是否可以获得成功状态。如果上传成功,我希望能够执行代码,否则执行其他代码。有什么建议么?

最佳答案

根据 AWS S3 JavaDoc:

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html#putObject%28com.amazonaws.services.s3.model.PutObjectRequest%29

Amazon S3 never stores partial objects; if during this call an exception wasn't thrown, the entire object was stored.



因此,成功返回的 PutObjectResult 对象表示调用成功(即对象已上传)。如果存在权限错误,则会抛出 AmazonServiceException:

AmazonServiceException - If any errors occurred in Amazon S3 while processing the request.



编辑 1 在上述评论中引用的答案中(建议 OP 下载刚刚上传的文件并将下载的副本与原始文件进行比较),您实际上是在为上传和下载付费,我认为这对于大多数使用来说都是多余的案例。相反,请使用 PutObjectResult 类中提供的信息,并验证是否使用该信息作为成功上传的证据。这不是精确的确认,但如果没有引发异常,您可能会依靠 S3 的成功保证。

请记住以下上传文件与流的限制。

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html#putObject%28com.amazonaws.services.s3.model.PutObjectRequest%29

When uploading a file:

  • The client automatically computes a checksum of the file. Amazon S3 uses checksums to validate the data in each file.

  • Using the file extension, Amazon S3 attempts to determine the correct content type and content disposition to use for the object.

When uploading directly from an input stream:

  • Be careful to set the correct content type in the metadata object before directly sending a stream. Unlike file uploads, content types from input streams cannot be automatically determined. If the caller doesn't explicitly set the content type, it will not be set in Amazon S3.

  • Content length must be specified before data can be uploaded to Amazon S3. Amazon S3 explicitly requires that the content length be sent in the request headers before it will accept any of the data. If the caller doesn't provide the length, the library must buffer the contents of the input stream in order to calculate it.

关于android - 如何使用 Amazon Web Services 获取上传成功状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35727469/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com