英文:
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槽并存储数据,但不会更改默认值。我尝试了谷歌提供的大多数选项,但无法解决这个问题。以下是代码片段:
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)
}
}
请注意,这只是代码的翻译部分,不包括任何其他内容。
英文:
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,"\n")
fmt.Printf("Created object %v at location %v\n\n", res.Name, res.SelfLink)
} else {
fmt.Printf("Objects.Insert failed: %v", err)
}
}
答案1
得分: 2
联系了Google技术支持。他们给出了以下答案,并且完美地解决了问题。
> 我找到了导致通过CS API设置的Content-type为"text/plain; charset=utf-8"而不是"text/csv; charset=utf-8"的根本原因。
>
> 如CLI-Go参考文档所述,函数Media():上传请求中使用的Content-Type标头将通过对r的内容进行嗅探来确定,除非提供了由googleapi.ContentType生成的MediaOption。
>
> 因此,要生成一个MediaOption,您需要调用googleapi.ContentType:
>
> 我通过导入"google.golang.org/api/googleapi"并按如下方式调用insert来修改了您的脚本:
>
> service.Objects.Insert(*bucketName,
> object).Media(file,googleapi.ContentType("text/csv;
> charset=utf-8")).Do();
>
> 看起来Media()会覆盖在Object上设置的数据。
英文:
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.
答案2
得分: 1
Metadata
是一个字符串 -> 字符串的映射,用于存储自定义的、用户定义的对象属性。你要找的是 ContentType
。
object := &storage.Object{Name: objectName, ContentType: "text/csv; charset=utf-8"}
翻译结果:
Metadata
是一个字符串 -> 字符串的映射,用于存储自定义的、用户定义的对象属性。你要找的是 ContentType
。
object := &storage.Object{Name: objectName, ContentType: "text/csv; charset=utf-8"}
英文:
Metadata
is a string -> string map of custom, user-defined object properties. What you're looking for is ContentType
.
object := &storage.Object{Name: objectName, ContentType: "text/csv; charset=utf-8"}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论