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