AWS S3映像上传

时间:2019-07-12 05:20:23

标签: swift xcode amazon-web-services amazon-s3

我有一段代码可以将图像上传到s3存储桶。问题是同一段代码(相同的键/配置)正在示例代码中工作,但是在完成确定的git commit 后,它在我的工作项目中不起作用。

现在开始质疑:是否有任何框架设置?我可以重置或尝试其他任何方法?

我注意到的另一个可疑之处是let transferManager = AWSS3TransferManager.default() 这行代码在代码无法正常工作的代码中向我发出警告Warning: AWSS3TransferManager is deprecated: Use AWSS3TransferUtility for upload and download operations.

即使在使用AWSS3TransferUtility后,我仍无法上传到图像,我正在使用的代码也在下面。

let credentialsProvider = AWSStaticCredentialsProvider(accessKey: accessKey, secretKey: secretKey)
    let configuration = AWSServiceConfiguration(region:AWSRegionType.USEast1, credentialsProvider:credentialsProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration
    let uploadRequest = AWSS3TransferManagerUploadRequest()!
    uploadRequest.body = localUrl
    uploadRequest.key = imageUrl.lastPathComponent
    uploadRequest.bucket = S3BucketName
    uploadRequest.contentType = "image/jpg"
    uploadRequest.acl = .publicRead
    let transferManager = AWSS3TransferManager.default()
    imageRequestGroup.enter()
    loader.Handler.show()
    transferManager.upload(uploadRequest).continueWith { [weak self] (task) -> Any? in
        DispatchQueue.main.async {
            loader.Handler.hide()
            if let error = task.error {
                print("Upload failed with error: (\(error.localizedDescription))")
            }
            if task.result != nil {
                let url = AWSS3.default().configuration.endpoint.url
                let publicURL = url?.appendingPathComponent(uploadRequest.bucket!).appendingPathComponent(uploadRequest.key!)
                if let absoluteString = publicURL?.absoluteString {
                    self?.imageUrlToSend = absoluteString.lastPathComponent
                }
            }
            self?.imageRequestGroup.leave()
        }
        return nil
    }

1 个答案:

答案 0 :(得分:0)

使用S3TransferUtility代替不推荐使用的TransferManager。 此处提供示例代码https://github.com/awslabs/aws-sdk-ios-samples/tree/master/S3TransferUtility-Sample/Swift