Tushar Das
Tushar Das

Reputation: 21

golang: Download S3 object with a versionId

I have an object stored in S3 which has multiple versions, I want to download a specific version of the object using Go, I'm using AWS SDK: https://github.com/aws/aws-sdk-go

I'm able to download an object from S3 without providing the versionId, i.e. the latest version of the object. But when I provide the versionId, I'm getting access denied error

Implementation as below:

sess, err := session.NewSession(&aws.Config{
    Region: aws.String(AWSRegion),
})
if err != nil {
    fmt.Println("Error creating session:", err)
    return nil, err
}

// Create an S3 service client
svc := s3.New(sess)

// Download the object
resp, err := svc.GetObject(&s3.GetObjectInput{
    Bucket: aws.String(AWSBucketName),
    Key:    aws.String(objKeyPath),
})

if err != nil {
    fmt.Println("Error downloading object:", err)
    return nil, err
}

respBytes, _ := io.ReadAll(resp.Body)

The above code works completely fine as versionId is not provided, but when I execute the below code providing the versionId, I get access denied error:

sess, err := session.NewSession(&aws.Config{
    Region: aws.String(AWSRegion),
})
if err != nil {
    fmt.Println("Error creating session:", err)
    return nil, err
}

// Create an S3 service client
svc := s3.New(sess)

// Download the object
resp, err := svc.GetObject(&s3.GetObjectInput{
    Bucket: aws.String(AWSBucketName),
    Key:    aws.String(objKeyPath),
    VersionId: aws.String(versionId),
})

if err != nil {
    fmt.Println("Error downloading object:", err)
    return nil, err
}

respBytes, _ := io.ReadAll(resp.Body)

Upvotes: 2

Views: 115

Answers (1)

Vishwa Ratna
Vishwa Ratna

Reputation: 6390

I converted the code to a lambda function and was able to exceute it successfully on aws.

package main

import (
    "context"
    "fmt"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
)

func bootstrap(ctx context.Context, event events.S3Event) {
    sess := session.Must(session.NewSession())
    svc := s3.New(sess)
    bucket := "stackoverflowbuc"
    key := "1706052153610.jpg"

    // Specify the version ID of the object that we want to fetch
    versionID := "ymJ0sde62voGEKnW4JNlrYlyKkCvHex9"
    params := &s3.GetObjectInput{
        Bucket:    aws.String(bucket),
        Key:       aws.String(key),
        VersionId: aws.String(versionID),
    }

    // Fetch the object with the specified version ID
    resp, err := svc.GetObject(params)
    if err != nil {
        fmt.Println("Error fetching object:", err)
        return
    }
    body := resp.Body
    defer body.Close()

    fmt.Println("Object fetched successfully with version ID:", versionID)
}

func main() {
    lambda.Start(bootstrap)
}

Below are the versioned objects in my s3 bucket:

enter image description here

Reseult of execution:

enter image description here

Upvotes: 2

Related Questions