duanbei7005 2016-04-05 07:59
浏览 32
已采纳

如何使用golang编辑云存储桶中对象的元数据信息

I've tried to insert a csv file from a local machine to the cloud storage bucket but it is storing as a text file. When i tried including Metadata option in

object := &storage.Object{Name: objectName, Metadata: map[string]string{"Content-Type": "text/csv; charset=utf-8"}}

It is creating another slot Content-Type and Storing the data but not changing the default value. Tried most of the options from google but unable to resolve this issue. Following is the code snippet.

package main

import (
        "flag"
        "fmt"
        "os"
        "golang.org/x/net/context"
        "golang.org/x/oauth2/google"
        storage "google.golang.org/api/storage/v1"
)

const (
        // This can be changed to any valid object name.
        objectName = "result"
        // This scope allows the application full control over resources in Google Cloud Storage
        scope = storage.DevstorageFullControlScope
)

var (
        projectID  = flag.String("project", "phani-1247 ", "Your cloud project ID.")
        bucketName = flag.String("bucket", "test-csvstorage", "The name of an existing bucket within your project.")
        fileName   = flag.String("file", "/home/phanikumar_dytha0/src/phani-1247/master/router/result.csv", "The file to upload.")
)



func main() {
        flag.Parse()
       client, err := google.DefaultClient(context.Background(), scope)
        if err!=nil{
            fmt.Printf("error")
        }
        // Insert an object into a bucket.
        object := &storage.Object{Name: objectName, Metadata: map[string]string{"Content-Type": "text/csv; charset=utf-8"}}
        file, err := os.Open(*fileName)
        if err!=nil{
            fmt.Printf("error")
        }
        service, err := storage.New(client)
        if err!=nil{
            fmt.Printf("error")
        }

        if res, err := service.Objects.Insert(*bucketName, object).Media(file).Do(); err == nil {
                //fmt.Printf("%v",res,"
")
                fmt.Printf("Created object %v at location %v

", res.Name, res.SelfLink)
        } else {
                fmt.Printf("Objects.Insert failed: %v", err)
        }


}
  • 写回答

2条回答 默认 最新

  • doupeng8419 2016-04-05 16:17
    关注

    Contacted Google Tech support. They gave the following answer and it worked perfectly.

    I found the root cause that explained why the Content-type set via CS API is "text/plain; charset=utf-8" instead of "text/csv; charset=utf-8".

    As desribed on the CLI-Go reference the function Media(): the Content-Type header used in the upload request will be determined by sniffing the contents of r, unless a MediaOption generated by googleapi.ContentType is supplied.

    So to generate a MediaOption you need to call to googleapi.ContentType:

    I modified your script by importing "google.golang.org/api/googleapi" and calling insert as follows:

    service.Objects.Insert(*bucketName,
    object).Media(file,googleapi.ContentType("text/csv;
    charset=utf-8")).Do();
    

    It seems that Media() overrides the data set on Object.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果