Amazon Aws iOS Sdk: How to List All File Names in a Folder

Amazon AWS IOS SDK: How to list ALL file names in a FOLDER

Amazon S3 is a "flat" file system, meaning it does not have physical folders. "Folders" as you know are simply prefixes added to file names.

You need to set the prefix property to your request (see the documentation)

For example, given the following set of files:

folderName1/fileNameA.txt

folderName1/fileNameB.txt

folderName2/folderName3/fileNameC.txt

If you set prefix with folderName1, and your delimiter with /, you should get only the first two entries.

Last, but not least, leave your bucketName only with the bucket name :)

More info at the S3 Developer Guide.

how to get file list inside of a folder in Amazon S3 Bucket

S3 is not flat, it just has only two levels: the bucket and the object name (it is flat inside the bucket). The bucket is always just the bucket. You're trying to add on to your bucket name, which produces an invalid bucket name.

The prefix and delimiter parameters are properties on S3ListObjectsRequest. My Objective-C is rusty, but I think you can set those properties with:

getListObjectsRequest.prefix = @"my/prefix/";
getListObjectsRequest.delimiter = @"/";

before you submit the request to S3.

Listing files in a specific folder of a AWS S3 bucket

Everything in S3 is an object. To you, it may be files and folders. But to S3, they're just objects.

Objects that end with the delimiter (/ in most cases) are usually perceived as a folder, but it's not always the case. It depends on the application. Again, in your case, you're interpretting it as a folder. S3 is not. It's just another object.

In your case above, the object users/<user-id>/contacts/<contact-id>/ exists in S3 as a distinct object, but the object users/<user-id>/ does not. That's the difference in your responses. Why they're like that, we cannot tell you, but someone made the object in one case, and didn't in the other. You don't see it in the AWS Management Console because the console is interpreting it as a folder and hiding it from you.

Since S3 just sees these things as objects, it won't "exclude" certain things for you. It's up to the client to deal with the objects as they should be dealt with.

Your Solution

Since you're the one that doesn't want the folder objects, you can exclude it yourself by checking the last character for a /. If it is, then ignore the object from the response.

Amazon AWS IOS SDK: How to download ALL files in a bucket without naming them individually


S3ListObjectsRequest *listObjectRequest = [[[S3ListObjectsRequest alloc] initWithName:"YourBucket"] autorelease];

S3ListObjectsResponse *listObjectResponse = [[AmazonClientManager s3] listObjects:listObjectRequest];

S3ListObjectsResult *listObjectsResults = listObjectResponse.listObjectsResult;

for (S3ObjectSummary *objectSummary in listObjectsResults.objectSummaries) {
NSLog(@"Bucket Contents %@ " ,[objectSummary key]);
}

Getting list of files in documents folder

This solution works with Swift 4 (Xcode 9.2) and also with Swift 5 (Xcode 10.2.1+):

let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
do {
let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
// process files
} catch {
print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
}

Here's a reusable FileManager extension that also lets you skip or include hidden files in the results:

import Foundation

extension FileManager {
func urls(for directory: FileManager.SearchPathDirectory, skipsHiddenFiles: Bool = true ) -> [URL]? {
let documentsURL = urls(for: directory, in: .userDomainMask)[0]
let fileURLs = try? contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil, options: skipsHiddenFiles ? .skipsHiddenFiles : [] )
return fileURLs
}
}

// Usage
print(FileManager.default.urls(for: .documentDirectory) ?? "none")

How to use AWS iOS SDK to delete a folder and all its objects in side bucket?

First of all, there is not such thing as "folders" in S3. Most S3 clients (including the AWS web console) show them as folders only for convenience (grouping stuff), but in fact, what you see as a "folder name" is merely a prefix.

Being that said, my suggestion to you is using the listObjectsInBucket API call, passing in your "folder name" as prefix in the S3ListObjectsRequest parameter.

When you have obtained all the keys (file names including prefix) matching that prefix, use the deleteObjects API call, passing in the keys in S3ListObjectsRequest parameter.

For more details on folder/prefix and deleting stuff, please see these related links:

Delete files, directories and buckets in amazon s3 java

Thread on AWS forum regarding this subject



Related Topics



Leave a reply



Submit