Search code examples
amazon-web-servicesgoamazon-s3aws-sdk-go

how to get only one object from a bucket?


I need to get only one object at a time from S3 bucket.. And I only found the API to get all the objects in a bucket.. is there a way to get only one? I'm gonna use a certain position or index to get one object at a time.

    result, err := w.Client.ListObjectsV2(ctx, input)
    if err != nil {
        fmt.Println("Got an error retrieving objects:")
        fmt.Println(err)
    }
    for _, item := range result.Contents {
        fmt.Println("Name:          ", *item.Key)
        fmt.Println("Last modified: ", *item.LastModified)
        fmt.Println("Size:          ", item.Size)
        fmt.Println("Storage class: ", item.StorageClass)
        fmt.Println("")
    }


Solution

  • You can request a maximum number of S3 keys to be returned by indicating MaxKeys=1 in your ListObjectsV2Input:

    MaxKeys: Sets the maximum number of keys returned in the response. By default the action returns up to 1,000 key names. The response might contain fewer keys but will never contain more.

    To get the next S3 key, you would use either StartAfter or ContinuationToken in your subsequent ListObjectsV2 request.