gogoogle-cloud-storagemetadatatype

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. 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)
        }


}

Solution

  • 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.