More instruction help on using Go Cloud Storage Client

Show More
RESOLVED
Fix:
 

 

//remove the io.writer argument requirement at top
func uploadFile(bucket, object string) error {
//...snip...
//remove the following at the bottom of the function
fmt.Fprintf(w, "Blob %v uploaded.\n", object)

//shoutout to Derek Perkins on the Go (Gophers) Slack 

 

 
Opinion: There needs to be many more examples of the client usage for Go end-to-end
 
hi all- new to go in general. i'm trying to make sense of the storage client sample script usage.I place the script into a sample file and then call the function 'uploadFile' from the main function as the driver via:
bucket := "mybucketname"
object := "myobjectname"
upploadFile(bucket, object)
The error is that I'm missing whatever w io.writer means:
not enough arguments in call to uploadFile
	have (string, string)
	want (io.Writer, string, string)
 I have no idea what it wants from me regarding that. Multiple blogs/tutorials state what it does and one even said it was implicit so it doesn't need a declaration.  The write object in the sample code looks like it's being created and then executed at the bottom of the function. If I try to remove the argument requirement in the function; I still get an error. Yet at the bottom it creates a new writer?
	// Upload an object with storage.Writer.
	wc := o.NewWriter(ctx)
	if _, err = io.Copy(wc, f); err != nil {
		return fmt.Errorf("io.Copy: %v", err)
	}
	if err := wc.Close(); err != nil {
		return fmt.Errorf("Writer.Close: %v", err)
	}
	fmt.Fprintf(w, "Blob %v uploaded.\n", object)
	return nil
Can someone show me how to actually use this or perform a very basic service account authenticated (I have the JSON file already) action to upload X local file to Y target bucket path?
0 1 167
1 REPLY 1

Full copy paste ready modified edition and example driver syntax to use for other Go noobs like myself:

func uploadFile(bucket, object string) error {
	// bucket := "bucket-name"
	// object := "object-name"
	ctx := context.Background()
	client, err := storage.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("storage.NewClient: %v", err)
	}
	defer client.Close()

	// Open local file.
	f, err := os.Open("./output.txt")
	if err != nil {
		return fmt.Errorf("os.Open: %v", err)
	}
	defer f.Close()

	ctx, cancel := context.WithTimeout(ctx, time.Second*50)
	defer cancel()

	o := client.Bucket(bucket).Object(object)

	// Optional: set a generation-match precondition to avoid potential race
	// conditions and data corruptions. The request to upload is aborted if the
	// object's generation number does not match your precondition.
	// For an object that does not yet exist, set the DoesNotExist precondition.
	o = o.If(storage.Conditions{DoesNotExist: true})
	// If the live object already exists in your bucket, set instead a
	// generation-match precondition using the live object's generation number.
	// attrs, err := o.Attrs(ctx)
	// if err != nil {
	//      return fmt.Errorf("object.Attrs: %v", err)
	// }
	// o = o.If(storage.Conditions{GenerationMatch: attrs.Generation})

	// Upload an object with storage.Writer.
	wc := o.NewWriter(ctx)
	if _, err = io.Copy(wc, f); err != nil {
		return fmt.Errorf("io.Copy: %v", err)
	}
	if err := wc.Close(); err != nil {
		return fmt.Errorf("Writer.Close: %v", err)
	}
	return nil
}

func main() {
	//test gcp storage client
	uploadFile("bucketname", "objectname")


}