简体   繁体   中英

Unable to download from subfolder of a bucket using AWS S3 iOS SDK

Using the AWS S3 SDK (via Cocoapods) v2.8.0 but I can't get it to download images from within a sub folder in the bucket. The code works fine for downloading within the root of the bucket

    func downloadData(imageName: String, completion: @escaping (Bool) -> () ) {
        let expression = AWSS3TransferUtilityDownloadExpression()
        expression.progressBlock = {(task, progress) in DispatchQueue.main.async(execute: {
            print("Download in process: \(progress.fractionCompleted*100)% complete")
        })
        }
        var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
        completionHandler = { (task, URL, data, error) -> Void in
            DispatchQueue.main.async(execute: {
                if let error = error?.localizedDescription {
                    print("Error in completion of download: \(error)")
                }
                print("Completion Response: \(task.response)")
                if let responseData = data {
                    if let image = UIImage.init(data: responseData) {
                        self.saveImage(imageName: imageName, image: image)
                        completion(true)
                    }
                }
                completion(false)
            })
        }
        transferUtility.downloadData(
            fromBucket: "myBucketName",
            key: "mySubFolder/" + imageName,
            expression: expression,
            completionHandler: completionHandler
            ).continueWith {
                (task) -> AnyObject? in if let error = task.error {
                    print("Error: \(error.localizedDescription)")
                }
                if let _ = task.result {
                }
                return nil;
        }
    }

I'm getting a 404 with the above code and the following error.

Error in completion of download: The operation couldn’t be completed. (com.amazonaws.AWSS3TransferUtilityErrorDomain error 2.)

Solved: The above code is correct but I was omitting the file extension from my key. In the root folder it didn't seem to be to fussy so would accept "ImageName" or "ImageName.jpg" but when adding the subfolder on it didn't like this and only accepted "SubFolder/ImageName.jpg".

Adding the file extension solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM