gpt4 book ai didi

iOS 图片上传到 AWS S3 Bucket 非常慢

转载 作者:可可西里 更新时间:2023-11-01 01:58:37 24 4
gpt4 key购买 nike

我正在尝试使用 AWS SDK 从我的 iOS 应用程序将图像上传到 AWS S3 存储桶。我使用以下方法上传图片:

func uploadMedia(mediaData: Data, mediaID: String) {

let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.SomeRegion,
identityPoolId:"my identity pool ...")
let configuration = AWSServiceConfiguration(region:.SomeRegion, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration

let transferManager = AWSS3TransferManager.default()

let uploadingFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(mediaID)
do {
try mediaData.write(to: uploadingFileURL, options: Data.WritingOptions.atomic)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}

let uploadRequest = AWSS3TransferManagerUploadRequest()!

uploadRequest.bucket = "myBucket"
uploadRequest.key = mediaID + ".jpg"
uploadRequest.body = uploadingFileURL
uploadRequest.contentType = "image/jpeg"

transferManager.upload(uploadRequest).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in

if let error = task.error as NSError? {
if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
switch code {
case .cancelled, .paused:
break
default:
print("Error uploading: \(uploadRequest.key!) Error: \(error)")
}
} else {
print("Error uploading: \(uploadRequest.key!) Error: \(error)")
}
return nil
}

let uploadOutput = task.result
print("Upload complete for: \(uploadRequest.key!)")
return nil
})
}

上传成功,但比仅通过 PHP 脚本上传到我的 EC2 实例中的目录花费的时间要长得多。即使对于只有 30 KB 左右的小文件,也需要多几秒钟的时间。我已经关闭了默认的服务器加密,但这并没有太大帮助。

我是否正确使用 SDK 上传图像数据?数据本地存储在 coreData 中,并作为数据而不是图像文件提取。这就是为什么我创建一个临时目录来创建用于上传的 url。

最佳答案

下面是基于 sqlbot 注释的代码:配置和传输管理器定义被移动到帮助程序类的初始化程序中。如果图片文件存储在文档文件系统中,则也不需要创建临时文件以获得更多优势,但我使用 coreData,因此需要保留它。

尽早在您的 viewController 中初始化 MediaHelper 的一个实例(让 mediaHelper = MediaHelper())并维护对它的引用,以便尽早完成初始化并且仅完成一次。

class MediaHelper {

var transferManager: AWSS3TransferManager!

init() {
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.SomeRegion,
identityPoolId:"my identity pool ...")
let configuration = AWSServiceConfiguration(region:.SomeRegion, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration

self.transferManager = AWSS3TransferManager.default()
}

func uploadMedia(mediaData: Data, mediaID: String) {

let uploadingFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(mediaID)
do {
try mediaData.write(to: uploadingFileURL, options: Data.WritingOptions.atomic)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}

let uploadRequest = AWSS3TransferManagerUploadRequest()!

uploadRequest.bucket = "myBucket"
uploadRequest.key = mediaID + ".jpg"
uploadRequest.body = uploadingFileURL
uploadRequest.contentType = "image/jpeg"

transferManager.upload(uploadRequest).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in

if let error = task.error as NSError? {
if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
switch code {
case .cancelled, .paused:
break
default:
print("Error uploading: \(uploadRequest.key!) Error: \(error)")
}
} else {
print("Error uploading: \(uploadRequest.key!) Error: \(error)")
}
return nil
}

let uploadOutput = task.result
print("Upload complete for: \(uploadRequest.key!)")
return nil
})
}

}

关于iOS 图片上传到 AWS S3 Bucket 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48595543/

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