英文:
S3 bucket creation with aws-sdk-go on local stack ends up with location :/none
问题
我一直在尝试使用golang和localstack学习S3存储桶上的CRUD操作。
使用以下代码:
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
var (
s3session *s3.S3
)
const (
REGION = "us-east-1"
Bucket_Name string = "sahil1234"
)
func init() {
s3session = s3.New(session.Must(session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
Endpoint: aws.String("http://localhost:4566/"),
})))
}
func listBuckets() (resp *s3.ListBucketsOutput) {
resp, err := s3session.ListBuckets(&s3.ListBucketsInput{})
if err != nil {
panic(err)
}
return resp
}
func createBucket() (resp *s3.CreateBucketOutput) {
resp, err := s3session.CreateBucket(&s3.CreateBucketInput{
// ACL: aws.String(s3.BucketCannedACLPrivate),
// ACL: aws.String(s3.BucketCannedACLPublicRead),
Bucket: aws.String(Bucket_Name),
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
LocationConstraint: aws.String(REGION),
},
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeBucketAlreadyExists:
fmt.Println("Bucket name already in use!")
panic(err)
case s3.ErrCodeBucketAlreadyOwnedByYou:
fmt.Println("Bucket exists and is owned by you!")
default:
panic(err)
}
}
}
return resp
}
func uploadObject(filename string) (resp *s3.PutObjectOutput) {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
fmt.Println("Uploading:", filename)
resp, err = s3session.PutObject(&s3.PutObjectInput{
Body: f,
Bucket: aws.String(Bucket_Name),
Key: aws.String(strings.Split(filename, "/")[1]),
ACL: aws.String(s3.BucketCannedACLPublicRead),
})
if err != nil {
panic(err)
}
return resp
}
func listObjects() (resp *s3.ListObjectsV2Output) {
resp, err := s3session.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(Bucket_Name),
})
if err != nil {
panic(err)
}
return resp
}
func getObject(filename string) {
fmt.Println("Downloading: ", filename)
resp, err := s3session.GetObject(&s3.GetObjectInput{
Bucket: aws.String(Bucket_Name),
Key: aws.String(filename),
})
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(resp.Body)
err = ioutil.WriteFile(filename, body, 0644)
if err != nil {
panic(err)
}
}
func deleteObject(filename string) (resp *s3.DeleteObjectOutput) {
fmt.Println("Deleting: ", filename)
resp, err := s3session.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(Bucket_Name),
Key: aws.String(filename),
})
if err != nil {
panic(err)
}
return resp
}
func main() {
fmt.Println(listBuckets())
fmt.Println(createBucket())
fmt.Println(listBuckets())
}
尽管createBucket
返回成功,但它并没有创建一个实际的存储桶,它只返回:
{
Location: "/None"
}
当我运行aws --endpoint-url=http://localhost:4566 s3 ls
时,存储桶也不会显示出来。
任何帮助将不胜感激!
输出:
{
Buckets: [{
CreationDate: 2021-09-29 07:25:21 +0000 UTC,
Name: "my-bucket1212"
}],
Owner: {
DisplayName: "webfile",
ID: "bcaf1ffd86f41161ca5fb16fd081034f"
}
}
{
Location: "/None"
}
{
Buckets: [{
CreationDate: 2021-09-29 07:25:21 +0000 UTC,
Name: "my-bucket1212"
}],
Owner: {
DisplayName: "webfile",
ID: "bcaf1ffd86f41161ca5fb16fd081034f"
}
}
PS:mybucket1212是我使用aws-cli创建的一个存储桶
英文:
I have been trying to learn CRUD operations on S3 buckets using golang and localstack
using the below code
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
var (
s3session *s3.S3
)
const (
REGION = "us-east-1"
Bucket_Name string = "sahil1234"
)
func init() {
s3session = s3.New(session.Must(session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
Endpoint: aws.String("http://localhost:4566/"),
})))
}
func listBuckets() (resp *s3.ListBucketsOutput) {
resp, err := s3session.ListBuckets(&s3.ListBucketsInput{})
if err != nil {
panic(err)
}
return resp
}
func createBucket() (resp *s3.CreateBucketOutput) {
resp, err := s3session.CreateBucket(&s3.CreateBucketInput{
// ACL: aws.String(s3.BucketCannedACLPrivate),
// ACL: aws.String(s3.BucketCannedACLPublicRead),
Bucket: aws.String(Bucket_Name),
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
LocationConstraint: aws.String(REGION),
},
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeBucketAlreadyExists:
fmt.Println("Bucket name already in use!")
panic(err)
case s3.ErrCodeBucketAlreadyOwnedByYou:
fmt.Println("Bucket exists and is owned by you!")
default:
panic(err)
}
}
}
return resp
}
func uploadObject(filename string) (resp *s3.PutObjectOutput) {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
fmt.Println("Uploading:", filename)
resp, err = s3session.PutObject(&s3.PutObjectInput{
Body: f,
Bucket: aws.String(Bucket_Name),
Key: aws.String(strings.Split(filename, "/")[1]),
ACL: aws.String(s3.BucketCannedACLPublicRead),
})
if err != nil {
panic(err)
}
return resp
}
func listObjects() (resp *s3.ListObjectsV2Output) {
resp, err := s3session.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(Bucket_Name),
})
if err != nil {
panic(err)
}
return resp
}
func getObject(filename string) {
fmt.Println("Downloading: ", filename)
resp, err := s3session.GetObject(&s3.GetObjectInput{
Bucket: aws.String(Bucket_Name),
Key: aws.String(filename),
})
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(resp.Body)
err = ioutil.WriteFile(filename, body, 0644)
if err != nil {
panic(err)
}
}
func deleteObject(filename string) (resp *s3.DeleteObjectOutput) {
fmt.Println("Deleting: ", filename)
resp, err := s3session.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(Bucket_Name),
Key: aws.String(filename),
})
if err != nil {
panic(err)
}
return resp
}
func main() {
fmt.Println(listBuckets())
fmt.Println(createBucket())
fmt.Println(listBuckets())
}
even tho the creatBucket returns succesfully, it does not create an actual bucket,
it just returns
{
Location: "/None"
}
The bucket also does not show up when i do aws --endpoint-url=http://localhost:4566 s3 ls
Any help would be much appreciated!
output:
{
Buckets: [{
CreationDate: 2021-09-29 07:25:21 +0000 UTC,
Name: "my-bucket1212"
}],
Owner: {
DisplayName: "webfile",
ID: "bcaf1ffd86f41161ca5fb16fd081034f"
}
}
{
Location: "/None"
}
{
Buckets: [{
CreationDate: 2021-09-29 07:25:21 +0000 UTC,
Name: "my-bucket1212"
}],
Owner: {
DisplayName: "webfile",
ID: "bcaf1ffd86f41161ca5fb16fd081034f"
}
}
PS: mybucket1212 is a bucket i created using aws-cli
答案1
得分: 4
现在最简单的选择是在配置中设置S3ForcePathStyle
:
S3ForcePathStyle: aws.Bool(true),
这是aws-cli
的做法。
但是,尽管没有确定的日期,AWS正在废弃这个选项,你可以在这里了解更多信息。
我会尝试找到一种在不使用这个选项的情况下使其工作的方法,并更新这个答案。
英文:
the easiest option now is to set S3ForcePathStyle
in your config:
S3ForcePathStyle: aws.Bool(true),
This is the way aws-cli
is doing it.
but this options is being deprecated by AWS even though without a set date.
I'll try and find a way to make it work without this option and update this answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论