Ios 如何使用Swift在本地保存Amazon S3文件?
我最近在做一个项目,通过AmazonS3服务上传和下载文件,特别是用户上传的个人资料图片。我也在使用Amazon Cognito(用户名、全名等),我喜欢Cognito的一个特点是,它将下载的数据保存在手机本地,甚至在应用程序离线时也会显示出来。然后,当应用程序上线时,它会更新任何数据更改。不幸的是,S3没有发生同样的情况。每次应用程序启动时,它都必须先从我的S3存储桶下载配置文件图片,然后再显示它们。我想做的是让应用程序从iOS设备上的S3存储桶中保存用户的个人资料图片,这样用户即使在应用程序脱机时也可以看到他们的图片。以下是我目前拥有的:Ios 如何使用Swift在本地保存Amazon S3文件?,ios,swift,amazon-web-services,amazon-s3,amazon-cognito,Ios,Swift,Amazon Web Services,Amazon S3,Amazon Cognito,我最近在做一个项目,通过AmazonS3服务上传和下载文件,特别是用户上传的个人资料图片。我也在使用Amazon Cognito(用户名、全名等),我喜欢Cognito的一个特点是,它将下载的数据保存在手机本地,甚至在应用程序离线时也会显示出来。然后,当应用程序上线时,它会更新任何数据更改。不幸的是,S3没有发生同样的情况。每次应用程序启动时,它都必须先从我的S3存储桶下载配置文件图片,然后再显示它们。我想做的是让应用程序从iOS设备上的S3存储桶中保存用户的个人资料图片,这样用户即使在应用程序
func downloadImage(){
var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
//downloading image
let S3BucketName: String = "*******"
let S3DownloadKeyName: String = self.credentialsProvider.identityId + "/profileImage.png"
let expression = AWSS3TransferUtilityDownloadExpression()
expression.downloadProgress = {(task: AWSS3TransferUtilityTask, bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) in
dispatch_async(dispatch_get_main_queue(), {
let progress = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
// self.statusLabel.text = "Downloading..."
NSLog("Progress is: %f",progress)
})
}
completionHandler = { (task, location, data, error) -> Void in
dispatch_async(dispatch_get_main_queue(), {
if ((error) != nil){
NSLog("Failed with error")
NSLog("Error: %@",error!);
//self.statusLabel.text = "Failed"
} else {
//self.statusLabel.text = "Success"
self.userProfile.image = UIImage(data: data!)
self.profileBackground.image = UIImage(data: data!)
}
})
}
let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility()
transferUtility.downloadToURL(nil, bucket: S3BucketName, key: S3DownloadKeyName, expression: expression, completionHander: completionHandler).continueWithBlock { (task) -> AnyObject! in
if let error = task.error {
NSLog("Error: %@",error.localizedDescription);
// self.statusLabel.text = "Failed"
}
if let exception = task.exception {
NSLog("Exception: %@",exception.description);
// self.statusLabel.text = "Failed"
}
if let _ = task.result {
// self.statusLabel.text = "Starting Download"
NSLog("Download Starting!")
// Do something with uploadTask.
}
return nil;
}
}
提前谢谢 这是一个从S3 Bucket下载图像并保存在本地电话上的帮助程序 下次如果你想看这张照片。助手将检查本地副本是否存在,如果不存在,将从S3下载
import UIKit
import AWSCore
import AWSS3
class S3Helper: NSObject {
@objc var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
@objc lazy var transferUtility = {
AWSS3TransferUtility.default()
}()
let DocDirPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
func downloadImage(imageView: UIImageView!, S3Folder: String, S3FileName: String) {
let S3DownloadKeyName: String = S3Folder + "/" + S3FileName
imageView.image = UIImage(named: "ic_place_holder.pdf")
let filePath = DocDirPath.appendingPathComponent(S3FileName).path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
let imageURL = self.DocDirPath.appendingPathComponent(S3FileName)
imageView.image = UIImage(contentsOfFile: imageURL.path)!
} else {
let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in
DispatchQueue.main.async(execute: {
})
}
self.completionHandler = { (task, location, data, error) -> Void in
DispatchQueue.main.async(execute: {
if let error = error {
NSLog("Failed with error: \(error)")
//self.statusLabel.text = "Failed"
}
else{
//self.statusLabel.text = "Success"
imageView.image = UIImage(data: data!)
do{
let imgPath = URL(fileURLWithPath: self.DocDirPath.appendingPathComponent(S3FileName).path)
try (imageView.image ?? UIImage(named: "ic_place_holder.pdf")!).jpegData(compressionQuality: 1.0)?.write(to: imgPath, options: .atomic)
} catch let error1{
print(error1.localizedDescription)
}
}
})
}
transferUtility.downloadData(
forKey: S3DownloadKeyName,
expression: expression,
completionHandler: completionHandler).continueWith { (task) -> AnyObject? in
if let error = task.error {
NSLog("Error: %@",error.localizedDescription);
DispatchQueue.main.async(execute: {
//self.statusLabel.text = "Failed"
})
}
if let _ = task.result {
DispatchQueue.main.async(execute: {
//self.statusLabel.text = "Downloading..."
})
NSLog("Download Starting!")
// Do something with uploadTask.
}
return nil;
}
}
}
}这是一个从S3存储桶下载图像并保存在本地电话上的帮助程序 下次如果你想看这张照片。助手将检查本地副本是否存在,如果不存在,将从S3下载
import UIKit
import AWSCore
import AWSS3
class S3Helper: NSObject {
@objc var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
@objc lazy var transferUtility = {
AWSS3TransferUtility.default()
}()
let DocDirPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
func downloadImage(imageView: UIImageView!, S3Folder: String, S3FileName: String) {
let S3DownloadKeyName: String = S3Folder + "/" + S3FileName
imageView.image = UIImage(named: "ic_place_holder.pdf")
let filePath = DocDirPath.appendingPathComponent(S3FileName).path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
let imageURL = self.DocDirPath.appendingPathComponent(S3FileName)
imageView.image = UIImage(contentsOfFile: imageURL.path)!
} else {
let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in
DispatchQueue.main.async(execute: {
})
}
self.completionHandler = { (task, location, data, error) -> Void in
DispatchQueue.main.async(execute: {
if let error = error {
NSLog("Failed with error: \(error)")
//self.statusLabel.text = "Failed"
}
else{
//self.statusLabel.text = "Success"
imageView.image = UIImage(data: data!)
do{
let imgPath = URL(fileURLWithPath: self.DocDirPath.appendingPathComponent(S3FileName).path)
try (imageView.image ?? UIImage(named: "ic_place_holder.pdf")!).jpegData(compressionQuality: 1.0)?.write(to: imgPath, options: .atomic)
} catch let error1{
print(error1.localizedDescription)
}
}
})
}
transferUtility.downloadData(
forKey: S3DownloadKeyName,
expression: expression,
completionHandler: completionHandler).continueWith { (task) -> AnyObject? in
if let error = task.error {
NSLog("Error: %@",error.localizedDescription);
DispatchQueue.main.async(execute: {
//self.statusLabel.text = "Failed"
})
}
if let _ = task.result {
DispatchQueue.main.async(execute: {
//self.statusLabel.text = "Downloading..."
})
NSLog("Download Starting!")
// Do something with uploadTask.
}
return nil;
}
}
}
}看看这个:这真的很有用,谢谢!但是,如果一个用户注销,另一个用户登录,该怎么办?照片在更新前是否仍会短暂显示,或者我的占位符图像是否仍会显示?再次感谢您的帮助。因此,在注销时,您希望清除应用程序数据路径中的所有内容。因此,基本上删除所有图像存储在其中的本地文件夹。如果您需要帮助,请查看此链接使用AWS登录用户系统。检查此链接:这非常有用,谢谢!但是,如果一个用户注销,另一个用户登录,该怎么办?照片在更新前是否仍会短暂显示,或者我的占位符图像是否仍会显示?再次感谢您的帮助。因此,在注销时,您希望清除应用程序数据路径中的所有内容。因此,基本上删除所有图像都存储在其中的本地文件夹。如果您需要帮助,请查看此链接,让用户使用AWS登录系统