// Use `dev_appserver.py --default_gcs_bucket_name GCS_BUCKET_NAME`// when running locally.bucket,err:=file.DefaultBucketName(ctx)iferr!=nil{log.Errorf(ctx,"failed to get default GCS bucket name: %v",err)}
写入 Cloud Storage
要将文件写入 Cloud Storage,请使用以下代码:
// createFile creates a file in Google Cloud Storage.func(d*demo)createFile(fileNamestring){fmt.Fprintf(d.w,"Creating file /%v/%v\n",d.bucketName,fileName)wc:=d.bucket.Object(fileName).NewWriter(d.ctx)wc.ContentType="text/plain"wc.Metadata=map[string]string{"x-goog-meta-foo":"foo","x-goog-meta-bar":"bar",}d.cleanUp=append(d.cleanUp,fileName)if_,err:=wc.Write([]byte("abcde\n"));err!=nil{d.errorf("createFile: unable to write data to bucket %q, file %q: %v",d.bucketName,fileName,err)return}if_,err:=wc.Write([]byte(strings.Repeat("f",1024*4)+"\n"));err!=nil{d.errorf("createFile: unable to write data to bucket %q, file %q: %v",d.bucketName,fileName,err)return}iferr:=wc.Close();err!=nil{d.errorf("createFile: unable to close bucket %q, file %q: %v",d.bucketName,fileName,err)return}}
// readFile reads the named file in Google Cloud Storage.func(d*demo)readFile(fileNamestring){io.WriteString(d.w,"\nAbbreviated file content (first line and last 1K):\n")rc,err:=d.bucket.Object(fileName).NewReader(d.ctx)iferr!=nil{d.errorf("readFile: unable to open file from bucket %q, file %q: %v",d.bucketName,fileName,err)return}deferrc.Close()slurp,err:=ioutil.ReadAll(rc)iferr!=nil{d.errorf("readFile: unable to read data from bucket %q, file %q: %v",d.bucketName,fileName,err)return}fmt.Fprintf(d.w,"%s\n",bytes.SplitN(slurp,[]byte("\n"),2)[0])iflen(slurp) > 1024{fmt.Fprintf(d.w,"...%s\n",slurp[len(slurp)-1024:])}else{fmt.Fprintf(d.w,"%s\n",slurp)}}
列出存储分区内容
此示例代码演示了如何列出存储分区的内容:
// listBucket lists the contents of a bucket in Google Cloud Storage.func(d*demo)listBucket(){io.WriteString(d.w,"\nListbucket result:\n")query:=&storage.Query{Prefix:"foo"}it:=d.bucket.Objects(d.ctx,query)for{obj,err:=it.Next()iferr==iterator.Done{break}iferr!=nil{d.errorf("listBucket: unable to list bucket %q: %v",d.bucketName,err)return}d.dumpStats(obj)}}
// deleteFiles deletes all the temporary files from a bucket created by this demo.func(d*demo)deleteFiles(){io.WriteString(d.w,"\nDeleting files...\n")for_,v:=ranged.cleanUp{fmt.Fprintf(d.w,"Deleting file %v\n",v)iferr:=d.bucket.Object(v).Delete(d.ctx);err!=nil{d.errorf("deleteFiles: unable to delete bucket %q, file %q: %v",d.bucketName,v,err)return}}}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-06。"],[[["This guide details how to interact with Cloud Storage for data storage and retrieval using the client library, assuming prerequisite setup tasks are completed."],["The document provides code snippets for essential operations including writing to Cloud Storage, which demonstrates the use of headers for request behavior, access control, and metadata."],["Instructions are provided on reading data back from Cloud Storage, including how to access files using their name, and the necessary steps to process the retrieved content."],["The content also covers how to list the contents of a specified Cloud Storage bucket and how to properly delete files from it."],["The guide explains the required imports, such as `google.golang.org/appengine`, `google.golang.org/appengine/file`, and `cloud.google.com/go/storage`, necessary to utilize Cloud Storage within an App Engine application."]]],[]]