我正在努力下载我在S3 Bucket中上传的特定文件。
我创建了一个名为“Photos”的存储桶,并上传了名为“test.png”的文件
在我的AppDelegate中设置我的CredentialProvider后,我尝试使用以下代码下载该文件:
let transferManager = AWSS3TransferManager.defaultS3TransferManager()
let downloadingFilePath = NSTemporaryDirectory().stringByAppendingString("test.png")
let downloadingFileUrl = NSURL(fileURLWithPath: downloadingFilePath)
let downloadRequest = AWSS3TransferManagerDownloadRequest()
downloadRequest.bucket = "photos"
downloadRequest.key = "test.png"
downloadRequest.downloadingFileURL = downloadingFileUrl
transferManager.download(downloadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (AWSTask) -> AnyObject! in
//Handle errors
if AWSTask.error != nil
{
print("Error downloading: \(AWSTask.error)")
// Retrive information important for later downloading
}
else
{
print("Download succesful..")
var uploadResult: AnyObject! = AWSTask.result
print("Upload result: \(uploadResult)")
let downloadOutput = AWSTask.result as! AWSS3TransferManagerDownloadOutput
}
return nil
})
但它一直给我错误:“您尝试访问的存储区必须使用指定的端点进行寻址。请将所有将来的请求发送到此端点”
我尝试将我的downloaderRequest.bucket更改为我在我的存储桶属性中找到的端点。 downloaderRequest.bucket =“photos.s3-website-sa-east-1.amazonaws.com”
但现在它说“指定的存储桶不存在,Code = NoSuchBucket”
我做错了什么想法?感谢。
答案 0 :(得分:1)
我终于明白了。 所以这里的问题是我在南美地区创造了我的水桶,亚马逊无法找到它。 甚至当我点击我的亚马逊地区时,它告诉我" S3不需要区域选择。"您需要在"美国标准"。
中创建它答案 1 :(得分:0)
在下载之前,请设置与访问AWS服务相关的所有必需信息,请先在您的应用代表中应用以下代码,
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let credentialsProvider = AWSStaticCredentialsProvider(accessKey: YOUR_AWS_ACCESS_KEY, secretKey: YOUR_AWS_SECRET_KEY)
let defaultServiceConfiguration = AWSServiceConfiguration(region: AWSRegionType.SAEast1, credentialsProvider: credentialsProvider) //Your region endpoint
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = defaultServiceConfiguration
AWSLogger.defaultLogger().logLevel = .Verbose
return true
}
注意:请注意区分大小写拼写的区分大小写。
func downloadFile()
{
let transferManager = AWSS3TransferManager.defaultS3TransferManager()
let downloadingFilePath = NSTemporaryDirectory().stringByAppendingString("test.png")
let downloadingFileURL = NSURL(fileURLWithPath: downloadingFilePath as String )
let downloadReadRequest : AWSS3TransferManagerDownloadRequest = AWSS3TransferManagerDownloadRequest()
downloadReadRequest.bucket = "photos"
downloadReadRequest.key = "test.png"
downloadReadRequest.downloadingFileURL = downloadingFileURL
let task = transferManager.download(downloadReadRequest)
task.continueWithBlock { (task) -> AnyObject? in
if task.error != nil
{
// Success
}
else
{
// Error
}
return nil
}
}
答案 2 :(得分:0)