Henrique da Costa
Henrique da Costa

Reputation: 370

Error downloading from S3 bucket on 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.

Upvotes: 0

Views: 2521

Answers (3)

Surendra Kumar
Surendra Kumar

Reputation: 657

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. enter image description here

if this is your bucket then , your regiontype in code must be : AWSRegionType.APSouth1

Upvotes: 0

Henrique da Costa
Henrique da Costa

Reputation: 370

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".

Upvotes: 1

Jigar Tarsariya
Jigar Tarsariya

Reputation: 3247

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
            }
}

Upvotes: 0

Related Questions