"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
endpoint := "https://tos-s3-cn-beijing.volces.com"
accessKey := os.Getenv("VOLCENGINE_ACCESS_KEY_ID")
secretKey := os.Getenv("VOLCENGINE_SECRET_ACCESS_KEY")
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, r string, options ...interface{}) (aws.Endpoint, error) {
HostnameImmutable: false,
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(region),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")),
config.WithEndpointResolverWithOptions(customResolver),
fmt.Printf("failed to load configuration, %v\n", err)
s3Client := s3.NewFromConfig(cfg, func(o *s3.Options) {
bucketName := "your-unique-bucket-name-" + strconv.FormatInt(time.Now().Unix(), 10)
objectKey := "my-object-key"
localFilePath := "local-file.txt"
downloadFilePath := "downloaded-file.txt"
err = ioutil.WriteFile(localFilePath, []byte("Hello, Volcengine TOS!"), 0644)
fmt.Printf("failed to create local file, %v\n", err)
defer os.Remove(localFilePath)
fmt.Printf("Creating bucket: %s\n", bucketName)
_, err = s3Client.CreateBucket(context.TODO(), &s3.CreateBucketInput{
Bucket: aws.String(bucketName),
fmt.Printf("failed to create bucket, %v\n", err)
fmt.Println("Bucket created successfully.")
fmt.Printf("Deleting bucket: %s\n", bucketName)
_, err = s3Client.DeleteBucket(context.TODO(), &s3.DeleteBucketInput{
Bucket: aws.String(bucketName),
fmt.Printf("failed to delete bucket, %v\n", err)
fmt.Println("Bucket deleted successfully.")
fmt.Printf("Uploading object to %s\n", bucketName)
file, err := os.Open(localFilePath)
fmt.Printf("failed to open file, %v\n", err)
_, err = s3Client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(objectKey),
fmt.Printf("failed to upload object, %v\n", err)
fmt.Println("Object uploaded successfully.")
fmt.Printf("Deleting object: %s\n", objectKey)
_, err = s3Client.DeleteObject(context.TODO(), &s3.DeleteObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(objectKey),
fmt.Printf("failed to delete object, %v\n", err)
fmt.Println("Object deleted successfully.")
fmt.Printf("Listing objects in bucket: %s\n", bucketName)
listOutput, err := s3Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
Bucket: aws.String(bucketName),
fmt.Printf("failed to list objects, %v\n", err)
for _, object := range listOutput.Contents {
fmt.Printf("- %s\n", *object.Key)
fmt.Printf("Downloading object: %s\n", objectKey)
getOutput, err := s3Client.GetObject(context.TODO(), &s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(objectKey),
fmt.Printf("failed to download object, %v\n", err)
defer getOutput.Body.Close()
body, err := ioutil.ReadAll(getOutput.Body)
fmt.Printf("failed to read object body, %v\n", err)
err = ioutil.WriteFile(downloadFilePath, body, 0644)
fmt.Printf("failed to write downloaded file, %v\n", err)
defer os.Remove(downloadFilePath)
fmt.Printf("Object downloaded successfully to %s\n", downloadFilePath)