Error downloading from S3 bucket on IOS. - ios

I'm having a hard time trying to Download a specific file I've uploaded in my S3 Bucket.
I created a bucket called "Photos" and uploaded the file named "test.png"
After setting my CredentialProvider in my AppDelegate I tried to make a download of that file with the following code:
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
})
But it keeps giving me the error: "The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint"
I tried to change my downloaderRequest.bucket to the endpoint I found in my bucket properties . downloaderRequest.bucket = "photos.s3-website-sa-east-1.amazonaws.com"
But now it says "The specified bucket does not exist, Code=NoSuchBucket"
Any ideas of what Ive be doing wrong? thanks.

I finally figure it out.
So the problem here is that I created my bucket in a South American region and Amazon couldn't find it.
Even thought when I click in my Amazon region it tell me that "S3 does not require region selection." You need to create it in the "US Standard".

Before downloading, please set all required information related to access AWS service, Please apply below code first in your app delegate,
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
}
NOTE: Please do care about bucket name spell with case sensitivity.
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
}
}

this problem occur because of region mismatch of s3 bucket and region we use while configuring s3
here is the code :
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: cognitoRegion,
identityPoolId: cognitoIdentityPoolId,
identityProviderManager: customIdentityProvider)
so, here region type must be same your bucket region type. Ex.
if this is your bucket then , your regiontype in code must be : AWSRegionType.APSouth1

Related

Using AWS Swift SDK to upload image to S3

I'm trying to upload an image to an S3 bucket using the putObject method in the AWS Swift SDK. I have followed the instructions here: https://docs.aws.amazon.com/sdk-for-swift/latest/developer-guide/examples-s3-objects.html
I am able to add strings to the s3 bucket, but can't figure out how to add images.
I have tried converting the images to jpegdata and pngdata to no avail.
Here is my code:
// AWS S3 image upload
func uploadFile(withImage image: UIImage) {
let s3Client = try? S3Client(region: "us-west-2")
let bucketName = "xxxxx"
let imageData = image.pngData()
guard let dataToUpload = "Text to upload working".data(using: .utf8) else {
return
}
let body = ByteStream.from(data: imageData!)
s3Client!.putObject(input: PutObjectInput(body: body, bucket: bucketName, contentType: "image/png", key: "Test")) { result in
switch(result) {
case .success(let response):
if let eTag = response.eTag {
print("Successfully uploaded the file with the etag: \(eTag)")
}
case .failure(let err):
print(err)
}
}
}
If I place "dataToUpload" into the ByteStream.from function, the data gets stored in the s3 bucket. If I use the imagedata, however, it does not.
Open to any and all solutions,
Thanks!!!
you can also Use AWSS3TransferUtility to upload files to S3
Steps
1)Setup bucket credential before uploading or simply in your app delegate by this method setupCreditial()
upload file to s3 by using the uploadFileToS3 method in your source view
/** regionType is your bucket region name identityPoolId is your bucket unique id*/
func setupCreditial(){
let credentialProvider = AWSCognitoCredentialsProvider(regionType: .AFSouth1, identityPoolId: "YourPoolID")
let config = AWSServiceConfiguration.init(region: .AFSouth1, credentialsProvider: credentialProvider)
AWSServiceManager.default().defaultServiceConfiguration = config
AWSS3TransferUtility.register(with: config!, forKey: "NAMEOFUTILITY")
}
/*
key is your aws3 key where you want to put image . in most cases it would public/key where key = any udid
content type is is file type like image,pdf,word etc
**/
public func uploadFileToS3(withKey key : String , contentType type : String, andData data : Data) {
let awsUploadExp = AWSS3TransferUtilityUploadExpression()
awsUploadExp.progressBlock = {task ,progress in
//progress goes here
}
guard let transferUtililty = AWSS3TransferUtility.s3TransferUtility(forKey: "NAMEOFUTILITY") else{
return
}
let completionHandler : AWSS3TransferUtilityUploadCompletionHandlerBlock = {(task,error)-> Void in
//handle your completion
}
transferUtililty.uploadData(data, bucket: "YourBucketname", key: key, contentType: type, expression: awsUploadExp, completionHandler: completionHandler)
}

How to use AWS Rekognition to Compare Face in Swift 3

I've been trying to use the AWSRekognition SDK in order to compare face. However, Amazon has no Documentation on how to integrate their SDK with iOS. They have links that show how to work with Recognition (Developer Guide) with examples only in Java and very limited.
I wanted to know if anyone knows how to integrate AWS Rekognition in Swift 3. How to Initialize it and make a request with an image, receiving a response with the labels.
I have AWS Signatures AccessKey, SecretKey, AWS Region, Service Name. also Body
{
"SourceImage": {
"S3Object": {
"Bucket": "bucketName",
"Name": "ios/sample.jpg"
}
},
"TargetImage": {
"S3Object": {
"Bucket": "buketName",
"Name": "ios/target.JPG"
}
}
}
how can I initialize Rekognition and build a Request.
Thanks you!
Instantiate the Rekognition Client, Here I'm using the client with the default configuration.
let rekognitionClient:AWSRekognition = AWSRekognition.default()
Otherwise, you can use the credentials as follows:
let credentialsProvider = AWSCognitoCredentialsProvider(
regionType: AWSRegionType.usEast2,
identityPoolId: "us-east-2_myPoolID")
let configuration = AWSServiceConfiguration(
region: AWSRegionType.usEast2,
credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
let rekognitionClient:AWSRekognition = AWSRekognition.default()
Now construct the request and set the image in it.
let image = UIImage(named: "MyImage")
let request = AWSRekognitionDetectLabelsRequest()
request.image = image
request.maxLabels = <num_labels_needed>
request.minConfidence = <confidence_interval_needed>
Now to compare faces, read about the CompareFacesRequest: https://github.com/aws/aws-sdk-ios/blob/master/AWSRekognition/AWSRekognitionService.m#L288
There is a sample test in the SDK that compares two faces in ObjC but you can translate that in Swift:
https://github.com/aws/aws-sdk-ios/blob/master/AWSRekognitionUnitTests/AWSGeneralRekognitionTests.m#L60
let key = "testCompareFaces"
let configuration = AWSServiceConfiguration(region: AWSRegionUSEast2, credentialsProvider: nil)
AWSRekognition.register(with: configuration, forKey: key)
AWSRekognition(for: key).compareFaces(AWSRekognitionCompareFacesRequest()).continue(withBlock: {(_ task: AWSTask) -> Any in
print("completed")
Swift 5.0
let key = "testCompareFaces"
let credentialsProvider = AWSStaticCredentialsProvider(accessKey: "Add_access_key_id", secretKey:"Add_secret_access_key_id")
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
AWSRekognition.register(with: configuration!, forKey: key)
let rekognition = AWSRekognition(forKey: key)
guard let request = AWSRekognitionCompareFacesRequest() else {
puts("Unable to initialize AWSRekognitionDetectLabelsRequest.")
return
}
let sourceImage = AWSRekognitionImage()
sourceImage!.bytes = sourceImage.jpegData(compressionQuality: 0.4)// Specify your source image
request.sourceImage = sourceImage
let targetImage = AWSRekognitionImage()
targetImage!.bytes = targetImage.jpegData(compressionQuality: 0.4) // Specify your target image
request.targetImage = targetImage
rekognition.compareFaces(request) { (respone, error) in
if error == nil {
if let response = respone {
if let first = response.faceMatches?.first {
print(first)
}
}
} else {
print(error?.localizedDescription)
}
}

Issue Integrating Amazon Simple Email Services API in Swift

I want to use the "verifyEmailIdentity" action which is defined in Objective-C as part of the Amazon Simple Email Service API but I'm having trouble doing so in Swift. I want to call the action in Swift code and have the documentation of the action defined in a pod but I'm not really sure how to go about this.
Here is some sample code but my program doesn't recognize the return type.
func createRequest(verifyEmailIdentityRequest: SESVerifyEmailIdentityRequest) -> AmazonServiceRequest {
var request: AmazonServiceRequest = SESRequest()
request.setParameterValue("VerifyEmailIdentity", forKey: "Action")
request.setParameterValue("2010-12-01", forKey: "Version")
request.delegate = verifyEmailIdentityRequest.delegate
request.credentials = verifyEmailIdentityRequest.credentials()
request.endpoint = verifyEmailIdentityRequest.requestEndpoint()
request.requestTag = verifyEmailIdentityRequest.requestTag()
if verifyEmailIdentityRequest != nil {
if verifyEmailIdentityRequest.emailAddress != nil {
request.setParameterValue("\ (verifyEmailIdentityRequest.emailAddress)", forKey: "\("EmailAddress")")
}
}
return request
}
http://docs.aws.amazon.com/ses/latest/APIReference/API_VerifyEmailIdentity.html
You can achieve that by using the following snippet:
func verifyEmailIdentity(verifyEmailIdentityRequest: AWSSESVerifyEmailIdentityRequest) {
// You should ideally set your configuration in app delegate
// Set the region and cognito pool id
let credentialsProvider = AWSCognitoCredentialsProvider(
regionType: AWSRegionType.Unknown,
identityPoolId: "YOUR_POOL_ID")
let configuration = AWSServiceConfiguration(
region: AWSRegionType.Unknown,
credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
let ses = AWSSES.defaultSES()
ses.verifyEmailIdentity(verifyEmailIdentityRequest).continueWithBlock { (task: AWSTask) -> AnyObject? in
if let error = task.error {
// handle error here
} else if let result = task.result {
// handle result here
}
return nil
}
}
Thanks,
Rohan

awss3transfermanager background uploads success but not in bucket

I am using AWSS3TransferManager to upload files directly to s3. I am getting a success message back from the upload block but it isn't appearing in my bucket in the s3 management console. When I visit the url directly it appears though. I need this to be added to the bucket because my app pulls from an sqs queue in order to create the object in the db.
Here is my code for uploading:
let uploadFileRequest = AWSS3TransferManagerUploadRequest()
uploadFileRequest.bucket = AWS.S3BucketName
uploadFileRequest.key = uploadFileName
uploadFileRequest.contentType = "video/quicktime"
uploadFileRequest.contentLength = NSNumber(integer: video!.length)
let transferManager = AWSS3TransferManager.defaultS3TransferManager()
uploadFileRequest.body = uploadFileURL!
transferManager.upload(uploadFileRequest).continueWithBlock({ (task) -> AnyObject! in
if task.error != nil {
print("video upload failed \(task.error)")
} else {
print("video upload succeeded")
print("\(uploadFileName)")
}
return 1
})
update:
I think this may be a security issue. Here is my code for setting up cognito:
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:
AWSRegionType.USEast1, identityPoolId: AWS.cognitoPoolID)
let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

Display UIImageView using Swift from AWS

I'm trying to render an image in UIImageView using Swift. The source object is located in an AWS S3 bucket. I could not find any example code on Google. So I tried to translate the code given in this link [AWS S3 SDK v2 for iOS - Download an image file to UIImage from Obj-C to Swift, but failed. I'm a beginner in iOS.
let accessKey = "ACCESS_CODE";
let secretKey = "SECRET_KEY";
// let credentialsProvider = AWSStaticCredentialsProvider.credentialsWithAccessKey(accessKey, secretKey: secretKey)
// ^ Xcode says - credentialsWithAccessKey is deprecated, use initWithAccessKey
let credentialsProvider = AWSStaticCredentialsProvider.initWithAccessKey(accessKey, secretKey: secretKey)
// ^ Xcode says - AWSStaticCredentialsProvider.Type does not have a member named ‘initWithAccessKey’
I could be doing very many things wrong here, even silly mistakes. The best help would be to point to some example code.
Have you tried like this
var credentialsProvider: AWSStaticCredentialsProvider = AWSStaticCredentialsProvider.credentialsWithAccessKey("MY_ACCESS_KEY", secretKey: "MY_SECRET_KEY")
var configuration: AWSServiceConfiguration = AWSServiceConfiguration.configurationWithRegion(AWSRegionUSWest1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
var transferManager: AWSS3 = AWSS3(configuration: configuration)
var getImageRequest: AWSS3GetObjectRequest = AWSS3GetObjectRequest.new()
getImageRequest.bucket = "MY_BUCKET"
getImageRequest.key = "MY_KEY"
transferManager.getObject(getImageRequest).continueWithExecutor(BFExecutor.mainThreadExecutor(), withBlock: {(task: BFTask) -> id in if task.error {
NSLog("Error: %#", task.error)
}
else {
NSLog("Got image")
var data: NSData = task.result.body()
dispatch_async(dispatch_get_main_queue(), {
var image: UIImage = UIImage.imageWithData(data)
myImageView.image = image
})
I created a custom class to download image. It worked for me.
class AWSImageDownloader {
init(AccessKey accessKey:String, SecretKey secretKey:String, andRegion region:AWSRegionType = .USEast1) {
let credentialsProvider = AWSStaticCredentialsProvider(accessKey: accessKey, secretKey: secretKey)
guard let configuration = AWSServiceConfiguration(region: region, credentialsProvider: credentialsProvider) else {
debugPrint("Failed to configure")
return
}
AWSServiceManager.default().defaultServiceConfiguration = configuration
}
func downloadImage(Name imageName:String, fromBucket bucketName:String, onDownload successCallback:#escaping AWSImageDownloadSuccess, andOnError errorCallback:#escaping AWSImageDownloadError){
let transferManager = AWSS3.default()
let getImageRequest = AWSS3GetObjectRequest()
getImageRequest?.bucket = bucketName
getImageRequest?.key = imageName
transferManager.getObject(getImageRequest!).continueWith(executor: AWSExecutor.mainThread()) { (anandt) -> Void in
if anandt.error == nil {
if let imageData = anandt.result?.body as? Data, let image = UIImage(data: imageData) {
successCallback(image)
} else {
errorCallback("Download failed")
}
} else {
let error = "Error \(anandt.error?.localizedDescription ?? "unknown by dev")"
errorCallback(error)
}
}
}
}

Resources