Go语言 使用官方SDK从华为云下载文件

z9smfwbn  于 2023-04-03  发布在  Go
关注(0)|答案(1)|浏览(236)

由于几天,我试图从华为下载文件,更准确地说是在他们的云存储.问题是,我还没有能够连接到它...我发现了一个来自华为的SDK:https://github.com/huaweicloud/huaweicloud-sdk-go-v3但是我对所有可以用来连接到它的协议都有点迷失,而且我每次都不能让一个工作。老实说,文档并没有真正帮助我。
我还发现了这个:https://github.com/huaweicloud/huaweicloud-sdk-go-obs有一个下载文件的例子,在这里,我连华为都连接不上...在AppGalery,项目设置中,我下载了配置文件,尝试了端点,但没有成功...
这是我尝试与obs(我知道/猜它应该是agc,但我还没有找到一个包),但不工作,由于主机...

/**
 * This sample demonstrates how to download an object
 * from OBS in different ways using the OBS SDK for Go.
 */
package huawei

import (
    "fmt"
    "io"

    "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)

type DownloadSample struct {
    bucketName string
    objectKey  string
    location   string
    obsClient  *obs.ObsClient
}

func newDownloadSample(ak, sk, endpoint, bucketName, objectKey, location string) *DownloadSample {
    obsClient, err := obs.New(ak, sk, endpoint)
    if err != nil {
        panic(err)
    }
    return &DownloadSample{obsClient: obsClient, bucketName: bucketName, objectKey: objectKey, location: location}
}

func (sample DownloadSample) GetObject() {
    input := &obs.GetObjectInput{}
    input.Bucket = sample.bucketName
    input.Key = sample.objectKey

    fmt.Printf("%+v\n", input)
    output, err := sample.obsClient.GetObject(input)
    if err != nil {
        panic(err)
    }
    defer func() {
        errMsg := output.Body.Close()
        if errMsg != nil {
            panic(errMsg)
        }
    }()
    fmt.Println("Object content:")
    body, err := io.ReadAll(output.Body)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(body))
    fmt.Println()
}

func RunDownloadSample() {
    const (
        endpoint   = "theEndPointInConfigJSONFile"
        ak         = "prettySureOfThis"
        sk         = "prettySureOfThis"
        bucketName = "prettySureOfThis"
        objectKey  = "test.txt" // a txt in the bucket to try to download it
        location   = ""
    )
    sample := newDownloadSample(ak, sk, endpoint, bucketName, objectKey, location)

    fmt.Println("Download object to string")
    sample.GetObject()
}

谢谢你的帮忙

mwkjh3gx

mwkjh3gx1#

可以试试官方的OBS SDK吗?

GO OBS SDK

https://support.huaweicloud.com/intl/en-us/sdk-go-devg-obs/obs_33_0001.html

不同下载类型:

https://support.huaweicloud.com/intl/en-us/sdk-go-devg-obs/obs_23_0501.html

正常下载+编码:

https://support.huaweicloud.com/intl/en-us/sdk-go-devg-obs/obs_23_0502.html

// Import a dependency.
import (
       "fmt"
       "obs"
)

var ak = "*** Provide your Access Key ***"
var sk = "*** Provide your Secret Key ***"
// Replace the following region with the one in use. Here uses the CN-Hong Kong region as an example.
var endPoint = "https://obs.ap-southeast-1.myhuaweicloud.com"

// Create an ObsClient struct.
var obsClient, _ = obs.New(ak, sk, endPoint)

func main() {
       input := &obs.GetObjectInput{}
       input.Bucket = "bucketname"
       input.Key = "objectname"
       output, err := obsClient.GetObject(input)
       if err == nil {
              defer output.Body.Close()
              fmt.Printf("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n",
                     output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified)
              p := make([]byte, 1024)
              var readErr error
              var readCount int
              // Read the object content.
              for {
                     readCount, readErr = output.Body.Read(p)
                     if readCount > 0 {
                           fmt.Printf("%s", p[:readCount])
                     }
                     if readErr != nil {
                           break
                     }
              }
       } else if obsError, ok := err.(obs.ObsError); ok {
              fmt.Printf("Code:%s\n", obsError.Code)
              fmt.Printf("Message:%s\n", obsError.Message)
       }
}

相关问题