julien2313
julien2313

Reputation: 339

Download a file from Huawei cloud using official SDK

I'm trying to download files from Huawei, more precisely from their cloud storage. The issue is, I have been unable to connect to it.

I found an SDK from Huawei, but I'm a little lost on all the protocol I can use to connect to it, and I haven't been able to make one working, each time. And the documentations are not very helpful.

I also found this. There is an example of downloading a file. Here, I can't even connect to Huawei. In the AppGalery, project settings, I downloaded the configuration file and tried the endpoint, but without success.

Here is what I tried with obs (I know/guess it should be agc, but I haven't found a package for it), but not working due to the host.

/**
 * 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()
}

Thank you for your help

Upvotes: 1

Views: 544

Answers (1)

David Sánchez
David Sánchez

Reputation: 606

May you try the official OBS SDK?

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

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

Normal download + Code:
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)
       }
}

Upvotes: 0

Related Questions