我尝试使用以下代码连接到IBM Cloud对象存储(Cleversafe),我收到以下错误
"错误回复:MissingRegion:找不到区域配置"
package main
import (
"fmt"
"os"
"bytes"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/aws/session"
)
func main() {
fmt.Printf("Starting ...")
aws_access_key_id := "some id"
aws_secret_access_key := "some key"
token := ""
creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key, token)
fmt.Printf("creds: ", creds)
_, err := creds.Get()
if err != nil {
fmt.Printf("bad credentials: %s", err)
}
//EndpointResolver endpoints.Resolver
cfg := aws.NewConfig().WithEndpoint("dal.ibmselect.objstor.com").WithCredentials(creds)
fmt.Printf("cfg: %+v \n", cfg)
//cfg := aws.NewConfig().WithEndpointResolver()
//cfg := aws.NewConfig().WithCredentials(creds)
svc := s3.New(session.New(), cfg)
fmt.Printf("svc: %+v \n", svc)
file, err := os.Open("./test.jpg")
if err != nil {
fmt.Printf("err opening file: %s", err)
}
defer file.Close()
fileInfo, _ := file.Stat()
size := fileInfo.Size()
buffer := make([]byte, size) // read file content to buffer
file.Read(buffer)
fileBytes := bytes.NewReader(buffer)
fileType := http.DetectContentType(buffer)
path := file.Name()
params := &s3.PutObjectInput{
Bucket: aws.String("cosv1-jlnab"),
Key: aws.String(path),
Body: fileBytes,
ContentLength: aws.Int64(size),
ContentType: aws.String(fileType),
}
resp, err := svc.PutObject(params)
if err != nil {
fmt.Printf("bad response: %s \n", err)
}
fmt.Printf("response %s", awsutil.StringValue(resp))
}
答案 0 :(得分:1)
感谢
以下是一个工作示例代码:
package main
import (
"fmt"
"os"
"bytes"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/aws/session"
)
func main() {
fmt.Printf("Starting ...")
aws_access_key_id := "Access key from Bluemix credentials"
aws_secret_access_key := "Secret key from Bluemix credentials"
token := ""
creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key, token)
fmt.Printf("creds: ", creds)
_, err := creds.Get()
if err != nil {
fmt.Printf("bad credentials: %s", err)
}
//EndpointResolver endpoints.Resolver
cfg := aws.NewConfig().WithRegion("cleversafe").WithEndpoint("s3-api.us-geo.objectstorage.softlayer.net").WithCredentials(creds)
fmt.Printf("cfg: %+v \n", cfg)
svc := s3.New(session.New(), cfg)
fmt.Printf("svc: %+v \n", svc)
file, err := os.Open("./test.jpg")
if err != nil {
fmt.Printf("err opening file: %s", err)
}
defer file.Close()
fileInfo, _ := file.Stat()
size := fileInfo.Size()
buffer := make([]byte, size) // read file content to buffer
file.Read(buffer)
fileBytes := bytes.NewReader(buffer)
fileType := http.DetectContentType(buffer)
path := file.Name()
params := &s3.PutObjectInput{
Bucket: aws.String("test-ophir"),
Key: aws.String(path),
Body: fileBytes,
ContentLength: aws.Int64(size),
ContentType: aws.String(fileType),
}
resp, err := svc.PutObject(params)
if err != nil {
fmt.Printf("bad response: %s \n", err)
}
fmt.Printf("response %s", awsutil.StringValue(resp))
}
答案 1 :(得分:0)
基本上这是因为IBM COS和AWS S3处理“区域”的概念有所不同。抛出此错误是因为AWS Go SDK不知道为用于计算授权标头的“region”变量提供什么值。在这种情况下,IBM COS并不关心什么值,只要它不是null。最简单的方法是通过命令行提供环境变量:export AWS_REGION=foo
其中foo
可以是任何字符串,尽管它也可以在应用程序中创建客户端时设置为配置变量。
我建议使用有意义的东西,例如us
或us-south
,因为IBM COS可能会在某个时候通过将“region”的概念链接到AWS来强制执行此操作。 {1}}变量,IBM COS当前使用该变量来定义存储区的存储类,尽管这种区别将在未来的某个时刻显示出来。