[英]How to edit the metadata information of an object in cloud storage bucket using 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.我试图将一个 csv 文件从本地机器插入到云存储桶,但它存储为文本文件。 When i tried including Metadata option in
当我尝试在其中包含元数据选项时
object := &storage.Object{Name: objectName, Metadata: map[string]string{"Content-Type": "text/csv; charset=utf-8"}} 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.它正在创建另一个插槽 Content-Type 并存储数据但不更改默认值。 Tried most of the options from google but unable to resolve this issue.
尝试了 google 的大部分选项,但无法解决此问题。 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,"\n")
fmt.Printf("Created object %v at location %v\n\n", res.Name, res.SelfLink)
} else {
fmt.Printf("Objects.Insert failed: %v", err)
}
}
Contacted Google Tech support.联系了 Google 技术支持。 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".
我找到了解释为什么通过 CS API 设置的 Content-type 是“text/plain; charset=utf-8”而不是“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.
正如CLI-Go 中所描述的,Media() 函数:上传请求中使用的 Content-Type 标头将通过嗅探 r 的内容来确定,除非提供了由 googleapi.ContentType 生成的 MediaOption。
So to generate a MediaOption you need to call to googleapi.ContentType :
因此,要生成 MediaOption,您需要调用googleapi.ContentType :
I modified your script by importing "google.golang.org/api/googleapi" and calling insert as follows:
我通过导入“google.golang.org/api/googleapi”并按如下方式调用插入来修改您的脚本:
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.
Media() 似乎覆盖了对象上的数据集。
Metadata
is a string -> string map of custom, user-defined object properties. Metadata
是一个字符串 -> 自定义的、用户定义的对象属性的字符串映射。 What you're looking for is ContentType
.您正在寻找的是
ContentType
。
object := &storage.Object{Name: objectName, ContentType: "text/csv; charset=utf-8"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.