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

How to edit the metadata information of an object in cloud storage bucket using golang

我尝试将一个 csv 文件从本地计算机插入云存储桶,但它存储为文本文件。当我尝试在

中包含元数据选项时

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

它正在创建另一个槽 Content-Type 并存储数据但不更改默认值。尝试了 google 中的大部分选项,但无法解决此问题。以下是代码片段。

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,"\n")
                fmt.Printf("Created object %v at location %v\n\n", res.Name, res.SelfLink)
        } else {
                fmt.Printf("Objects.Insert failed: %v", err)
        }


}

Metadata 是自定义的、用户定义的对象属性的字符串 -> 字符串映射。您要查找的是 ContentType.

object := &storage.Object{Name: objectName, ContentType: "text/csv; charset=utf-8"}

已联系 Google 技术支持。他们给出了以下答案并且效果很好。

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.