我正在尝试从华为下载文件,更准确地说是从他们的云存储下载文件。问题是,我无法连接到它。
我从华为找到了一个SDK,但我对我可以用来连接它的所有协议都有点迷路,而且我每次都不能使一个工作。而且这些文档也不是很有帮助。
我也发现了这个。这里有一个下载文件的例子。在这里,我甚至不能连接华为。在appgallery的项目设置中,我下载了配置文件并尝试了端点,但没有成功。
这是我用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("%+vn", 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()
}
谢谢你的帮助
你可以试试官方的OBS SDK吗?
OBS SDK for GOhttps://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:%sn",
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:%sn", obsError.Code)
fmt.Printf("Message:%sn", obsError.Message)
}
}