本文整理汇总了Golang中google/golang.org/cloud/storage.Client.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.Close方法的具体用法?Golang Client.Close怎么用?Golang Client.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google/golang.org/cloud/storage.Client
的用法示例。
在下文中一共展示了Client.Close方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: storeImage
func storeImage(rw http.ResponseWriter, req *http.Request) {
// Appengine
var c appengine.Context
// Google Cloud Storage authentication
var cc gcscontext.Context
// Google Cloud Storage bucket name
var bucketName string = ""
// Google Cloud Storage client
var client *storage.Client
// Google Cloud Storage bucket
var bucketHandle *storage.BucketHandle
// User uploaded image file name
var fileName string = uuid.New()
// Transform user uploaded image to a thumbnail file name
var fileNameThumbnail string = uuid.New()
// User uploaded image file type
var contentType string = ""
// User uploaded image file raw data
var b []byte
// Google Cloud Storage file writer
var wc *storage.Writer = nil
// Error
var err error = nil
// Result, 0: success, 1: failed
var r int = http.StatusCreated
// Set response in the end
defer func() {
// Return status. WriteHeader() must be called before call to Write
if r == http.StatusCreated {
// Changing the header after a call to WriteHeader (or Write) has no effect.
// rw.Header().Set("Location", req.URL.String()+"/"+cKey.Encode())
rw.Header().Set("Location", "http://"+bucketName+".storage.googleapis.com/"+fileName)
rw.Header().Set("X-Thumbnail", "http://"+bucketName+".storage.googleapis.com/"+fileNameThumbnail)
rw.WriteHeader(r)
} else {
http.Error(rw, http.StatusText(r), r)
}
}()
// To log information in Google APP Engine console
c = appengine.NewContext(req)
// Get data from body
b, err = ioutil.ReadAll(req.Body)
if err != nil {
c.Errorf("%s in reading body", err)
r = http.StatusInternalServerError
return
}
c.Infof("Body length %d bytes, read %d bytes", req.ContentLength, len(b))
// Determine filename extension from content type
contentType = req.Header["Content-Type"][0]
switch contentType {
case "image/jpeg":
fileName += ".jpg"
fileNameThumbnail += ".jpg"
default:
c.Errorf("Unknown or unsupported content type '%s'. Valid: image/jpeg", contentType)
r = http.StatusBadRequest
return
}
c.Infof("Content type %s is received, %s is detected.", contentType, http.DetectContentType(b))
// Prepare Google Cloud Storage authentication
cc = gcsappengine.NewContext(req)
if client, err = storage.NewClient(cc); err != nil {
c.Errorf("%s in initializing a GCS client", err)
r = http.StatusInternalServerError
return
}
defer client.Close()
// Get default bucket
if bucketName, err = gcsfile.DefaultBucketName(cc); err != nil {
c.Errorf("%s in getting default GCS bucket name", err)
r = http.StatusInternalServerError
return
}
bucketHandle = client.Bucket(bucketName)
c.Infof("APP Engine Version: %s", gcsappengine.VersionID(cc))
c.Infof("Using bucket name: %s", bucketName)
// Change default object ACLs
if err = bucketHandle.DefaultObjectACL().Set(cc, storage.AllUsers, storage.RoleReader); err != nil {
c.Errorf("%v in saving default object ACL rule for bucket %q", err, bucketName)
r = http.StatusInternalServerError
return
}
// Store rotated image in Google Cloud Storage
var in *bytes.Reader = bytes.NewReader(b)
var x *exif.Exif = nil
var orientation *tiff.Tag = nil
var beforeImage image.Image
var afterImage *image.NRGBA = nil
// Read EXIF
if _, err = in.Seek(0, 0); err != nil {
//.........这里部分代码省略.........