Home > Backend Development > Golang > AWS S3 ListObjectsV2 returns 'folder' as object

AWS S3 ListObjectsV2 returns 'folder' as object

WBOY
Release: 2024-02-08 20:53:49
forward
704 people have browsed it

AWS S3 ListObjectsV2 返回“文件夹”作为对象

Question content

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)
Copy after login

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/"
}
Copy after login

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)
Copy after login

But the result is the same.

This is the result of the parameters:

Params:  {
  Bucket: "BUCKET",
  Prefix: "Folder1/Folder2/Folder3/"
}
Copy after login


Correct answer


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"
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template