I'm trying to list all files under a specific directory in an s3 bucket (I know s3 doesn't really have a directory.).
The structure is:
bucket-name: folder1/folder2/folder3/file
So in the s3 world I believe this would just store a file named above.
This is my code:
params := &s3.listobjectsv2input{ bucket: aws.string(os.getenv("s3_user_bucket")), prefix: aws.string(key + loc[0] + "/"), } resp, _ := svc.listobjectsv2(params)
resp
contains the file I expect, and the "directory" it is stored in:
contents: [{ etag: "\"tag\"", key: "folder1/folder2/folder3/", lastmodified: 2023-03-02 17:32:17 +0000 utc, size: 0, storageclass: "standard" },{ etag: "\"tag\"", key: "folder1/folder2/folder3/file", lastmodified: 2023-03-02 17:32:30 +0000 utc, size: 106808, storageclass: "standard" }], istruncated: false, keycount: 2, maxkeys: 1000, name: "bucket", prefix: "folder1/folder2/folder3/" }
I also tried:
params := &s3.listobjectsv2input{ bucket: aws.string(os.getenv("s3_user_bucket")), prefix: aws.string(key + loc[0] + "/"), delimiter: aws.string("/"), } resp, _ := svc.listobjectsv2(params)
But the result is the same.
This is the result of the parameters:
Params: { Bucket: "BUCKET", Prefix: "Folder1/Folder2/Folder3/" }
I assume you are asking why there is a directory returned as an object.
You are absolutely right, amazon s3 does not use directories . Instead, the object's filename (key
) is the full path, including the filename. There is no need to create a directory before creating an object on a specific path.
However, if someone clicks the Create Folder button in the s3 management console, it will create a zero-length object with the name of the directory. This "forces" the directory to appear (because there is an object in it), but it is actually a zero-length object.
View your listing results:
Contents: [{ ETag: "\"TAG\"", Key: "Folder1/Folder2/Folder3/", LastModified: 2023-03-02 17:32:17 +0000 UTC, Size: 0, StorageClass: "STANDARD"
You'll notice that it says size: 0
, so this is a zero-length object.
If you want to omit these types of files from the list, just skip the zero-length objects in your code.
The above is the detailed content of AWS S3 ListObjectsV2 returns 'folder' as object. For more information, please follow other related articles on the PHP Chinese website!