如何将文件上传到 Amazon S3 并使用 iOS 获取 url 作为响应

How to upload a file to Amazon S3 and get the url in response using iOS

我使用了亚马逊示例代码中的代码片段。下面的代码将数据上传到 amazon S3 存储桶中,但我需要上传文件的 URL。我正朝着正确的方向前进吗?或者有人可以指出我犯的错误

如有任何帮助,我们将不胜感激。

AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
    AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
    uploadRequest.bucket = S3BucketName;
    uploadRequest.key = S3UploadKeyName;
    uploadRequest.body = self.uploadFileURL;
    //uploadRequest.contentLength = [NSNumber numberWithUnsignedLongLong:fileSize];

    [[transferManager upload:uploadRequest] continueWithBlock:^id(BFTask *task) {
        // Do something with the response
        AWSS3TransferManagerUploadOutput *uploadOutPut = task.result;
        NSLog(@"bftask:%@",uploadOutPut); // Upload out put gives me the following response
        return nil;
    }];

bf任务响应:

bftask:<AWSS3TransferManagerUploadOutput: 0x1706606c0> {
    ETag = "\"0aefedfa36b687a74025b1ad50f3101f\"";
    serverSideEncryption = 0;
}

如果您只需要下载文件,可以使用AWSS3TransferManagerDownloadRequest使用SDK下载文件。或者,您可以使用 AWSS3PreSignedURLBuilder 生成预签名 URL 来下载文件。

如果您只想知道对象的 URL,则 URL 遵循以下模式:

https://<YourS3Endpoint>/<YourBucketName>/<YourObjectKeyName>

默认情况下,Amazon S3 中的任何对象都是私有的,不可公开读取。如果你想让它公开可读,你需要在你的 AWSS3TransferManagerUploadRequest 对象上设置 ACL

有关详细信息,请参阅 Amazon S3 Bucket Public Access Considerations