0

我正在尝试使用以下代码连接到 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))
}
4

2 回答 2

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))
}
于 2017-06-09T11:08:29.750 回答
0

本质上,这是因为 IBM COS 和 AWS S3 处理“区域”概念的方式略有不同。引发此错误是因为 AWS Go 开发工具包不知道为用于计算授权标头的“区域”变量提供什么值。在这种情况下,IBM COS 不关心什么值,只要它不为空即可。最简单的方法是通过命令行提供环境变量:export AWS_REGION=foowherefoo可以是任何字符串,但也可以在应用程序中创建客户端时将其设置为配置变量。

我建议使用一些有意义的东西,比如usIBM us-southCOS 可能会通过将“区域”的概念链接到 AWSLocationConstraint变量来强制执行此操作,IBM COS 目前使用该变量来定义存储类一个桶,尽管这种区别将在将来的某个时候明确。

于 2017-06-07T21:45:24.233 回答