本文整理汇总了Golang中google/golang.org/appengine/file.DefaultBucketName函数的典型用法代码示例。如果您正苦于以下问题:Golang DefaultBucketName函数的具体用法?Golang DefaultBucketName怎么用?Golang DefaultBucketName使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DefaultBucketName函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Store
// Store binary data to GCS
func Store(c context.Context, data []byte, fileName, mimeType, bucketName string) (absFilename string, err error) {
if bucketName == "" {
var err error
if bucketName, err = file.DefaultBucketName(c); err != nil {
log.Errorf(c, "failed to get default GCS bucket name: %v", err)
return "", err
}
}
client, err := storage.NewClient(c)
if err != nil {
log.Errorf(c, "failed to create storage client: %v", err)
return "", err
}
defer client.Close()
wc := client.Bucket(bucketName).Object(fileName).NewWriter(c)
wc.ContentType = mimeType
if _, err := wc.Write(data); err != nil {
log.Errorf(c, "upload file: unable to write data to bucket %q, file %q: %v", bucketName, fileName, err)
return "", err
}
if err := wc.Close(); err != nil {
log.Errorf(c, "upload file: unable to close bucket %q, file %q: %v", bucketName, fileName, err)
return "", err
}
return getAbsFilename(bucketName, fileName), nil
}
示例2: handler
// handler is the main demo entry point that calls the GCS operations.
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
c := appengine.NewContext(r)
if bucket == "" {
var err error
if bucket, err = file.DefaultBucketName(c); err != nil {
log.Errorf(c, "failed to get default GCS bucket name: %v", err)
return
}
}
hc := &http.Client{
Transport: &oauth2.Transport{
Source: google.AppEngineTokenSource(c, storage.ScopeFullControl),
// Note that the App Engine urlfetch service has a limit of 10MB uploads and
// 32MB downloads.
// See https://cloud.google.com/appengine/docs/go/urlfetch/#Go_Quotas_and_limits
// for more information.
Base: &urlfetch.Transport{Context: c},
},
}
ctx := cloud.NewContext(appengine.AppID(c), hc)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "Demo GCS Application running from Version: %v\n", appengine.VersionID(c))
fmt.Fprintf(w, "Using bucket name: %v\n\n", bucket)
d := &demo{
c: c,
w: w,
ctx: ctx,
}
n := "demo-testfile-go"
d.createFile(n)
d.readFile(n)
d.copyFile(n)
d.statFile(n)
d.createListFiles()
d.listBucket()
d.listBucketDirMode()
d.defaultACL()
d.putDefaultACLRule()
d.deleteDefaultACLRule()
d.bucketACL()
d.putBucketACLRule()
d.deleteBucketACLRule()
d.acl(n)
d.putACLRule(n)
d.deleteACLRule(n)
d.deleteFiles()
if d.failed {
io.WriteString(w, "\nDemo failed.\n")
} else {
io.WriteString(w, "\nDemo succeeded.\n")
}
}
示例3: handler
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
ctx := appengine.NewContext(r)
if bucket == "" {
var err error
if bucket, err = file.DefaultBucketName(ctx); err != nil {
log.Errorf(ctx, "error", err)
return
}
}
client, err := storage.NewClient(ctx)
if err != nil {
log.Errorf(ctx, "error", err)
return
}
defer client.Close()
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "Demo GCS Application running from Version: %v\n", appengine.VersionID(ctx))
fmt.Fprintf(w, "Using bucket name: %v\n\n", bucket)
d := &demo{
w: w,
ctx: ctx,
client: client,
bucket: client.Bucket(bucket),
}
n := "demo-testfile-go"
d.makeF(n)
d.rFile(n)
d.cFile(n)
d.statFile(n)
d.createListFiles()
d.listBucket()
d.listBucketDirMode()
d.defaultACL()
d.putDefaultACLRule()
d.deleteDefaultACLRule()
d.bucketACL()
d.putBucketACLRule()
d.deleteBucketACLRule()
d.acl(n)
d.putACLRule(n)
d.deleteACLRule(n)
d.deleteFiles()
if d.failed {
io.WriteString(w, "\nDemo failed.\n")
} else {
io.WriteString(w, "\nDemo succeeded.\n")
}
}
示例4: handler
// handler is the main demo entry point that calls the GCS operations.
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
c := appengine.NewContext(r)
bucketName, err := file.DefaultBucketName(c)
if err != nil {
c.Errorf("failed to get default GCS bucket name: %v", err)
return
}
config := google.NewAppEngineConfig(c, storage.ScopeFullControl)
ctx := cloud.NewContext(appengine.AppID(c), &http.Client{Transport: config.NewTransport()})
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "Demo GCS Application running from Version: %v\n", appengine.VersionID(c))
fmt.Fprintf(w, "Using bucket name: %v\n\n", bucketName)
d := &demo{
c: c,
w: w,
ctx: ctx,
bucket: bucketName,
}
n := "demo-testfile-go"
d.createFile(n)
d.readFile(n)
d.copyFile(n)
d.statFile(n)
d.createListFiles()
d.listBucket()
d.listBucketDirMode()
d.defaultACL()
d.putDefaultACLRule()
d.deleteDefaultACLRule()
d.bucketACL()
d.putBucketACLRule()
d.deleteBucketACLRule()
d.acl(n)
d.putACLRule(n)
d.deleteACLRule(n)
d.deleteFiles()
if d.failed {
io.WriteString(w, "\nDemo failed.\n")
} else {
io.WriteString(w, "\nDemo succeeded.\n")
}
}
示例5: Writer
func Writer(filename string, r *http.Request) (*storage.Writer, error) {
c := appengine.NewContext(r)
bucket, err := file.DefaultBucketName(c)
if err != nil {
log.Errorf(c, "Failed to get default bucket: %v", err)
return nil, err
}
ctx, err := auth(r)
if err != nil {
log.Errorf(c, "Failed to get context: %v", err)
return nil, err
}
w := storage.NewWriter(ctx, bucket, filename)
return w, nil
}
示例6: handler
func handler(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
http.NotFound(res, req)
return
}
//[START get_default_bucket]
ctx := appengine.NewContext(res)
if bucket == "" {
var err error
if bucket, err = file.DefaultBucketName(ctx); err != nil {
log.Errorf(ctx, "failed to get default GCS bucket name: %v", err)
return
}
}
//[END get_default_bucket]
//Make Client
client, err := storage.NewClient(ctx)
if err != nil {
log.Errorf(ctx, "failed to get default GCS bucket name: %v", err)
return
}
defer client.Close()
//Construct our object to interface with the cloud storage.
res.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(res, "Demo GCS Application running from Version: %v\n", appengine.VersionID(ctx))
fmt.Fprintf(res, "Using bucket name: %v\n\n", bucket)
d := &demo{
w: res, //The response writer so it knows what to write to later if retrieving.
ctx: ctx, //Current Context
client: client, //Current Client
bucket: client.Bucket(bucket), //Relevant Bucket, bucket is a global string that contains the default bucket location.
}
//The name of the file we will be creating.
n := "demo-testfile-go"
//Create the File and store in google cloud.
d.createFile(n)
d.listBucket()
}
示例7: handleListBucket
// handleListBucket lists the contents of the default bucket
func handleListBucket(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
if bucket == "" {
var err error
if bucket, err = file.DefaultBucketName(ctx); err != nil {
log.Errorf(ctx, "failed to get default GCS bucket name: %v", err)
return
}
}
client, err := storage.NewClient(ctx)
if err != nil {
log.Errorf(ctx, "failed to get default GCS bucket name: %v", err)
return
}
defer client.Close()
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "Demo GCS Application running from Version: %v\n", appengine.VersionID(ctx))
fmt.Fprintf(w, "Using bucket name: %v\n\n", bucket)
writer := client.Bucket(bucket).Object("foo").NewWriter(ctx)
_, err = writer.Write([]byte("Yoho!"))
if err != nil {
log.Errorf(ctx, "Error while creating file")
}
log.Infof(ctx, "Created a new file. So yay!")
reader, err := client.Bucket(bucket).Object("foo").NewReader(ctx)
if err != nil {
log.Errorf(ctx, "Error creating reader for object from Bucket. %s", err)
}
content, err := ioutil.ReadAll(reader)
if err != nil {
log.Errorf(ctx, "Error reading object")
}
fmt.Fprintf(w, "Got: %s", &content)
}
示例8: Delete
// Delete removes an object by name from the bucket being used. If the object does not
// exist and there is nothing to delete, Delete returns with no error.
// TODO Delete thumbnail and web view copies too!
func Delete(filename string, r *http.Request) error {
c := appengine.NewContext(r)
bucket, err := file.DefaultBucketName(c)
if err != nil {
log.Errorf(c, "Failed to get default bucket: %v", err)
return err
}
ctx, err := auth(r)
if err != nil {
log.Errorf(c, "Failed to get context: %v", err)
return err
}
log.Infof(c, "Attempting to delete file %v from bucket %v.", filename, bucket)
// StatObject is used here to check existence before calling DeleteObject.
// If the object does not exist, DeleteObject returns an error that is NOT
// ErrObjectNotExist, so it seemed more reliable to check with StatObject first...
_, err = storage.StatObject(ctx, bucket, filename)
if err == storage.ErrObjectNotExist {
log.Warningf(c, "Object does not exist, nothing to delete.")
return nil
}
err = storage.DeleteObject(ctx, bucket, filename)
if err != nil {
log.Errorf(c, "Failed to delete file.")
log.Infof(c, "Attempting to remove public access to file...")
aclErr := storage.DeleteACLRule(ctx, bucket, filename, storage.AllUsers)
if aclErr != nil {
log.Errorf(c, "Failed to remove public file access!")
} else {
log.Infof(c, "File access removed.")
}
return err
}
return nil
}
示例9: serveFilesUpload
func serveFilesUpload(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
serveAPI(res, req, func() interface{} {
ctx := appengine.NewContext(req)
session, _ := sessionStore.Get(req, "session")
_, ok := session.Values["email"].(string)
if !ok {
return HTTPError{403, "access denied"}
}
bucket, err := file.DefaultBucketName(ctx)
if err != nil {
return err
}
hc := &http.Client{
Transport: &oauth2.Transport{
Source: google.AppEngineTokenSource(ctx, storage.ScopeFullControl),
Base: &urlfetch.Transport{Context: ctx},
},
}
id := uuid.NewV1().String()
ff, _, err := req.FormFile("file")
if err != nil {
return err
}
defer ff.Close()
cctx := cloud.NewContext(appengine.AppID(ctx), hc)
wc := storage.NewWriter(cctx, bucket, id)
io.Copy(wc, ff)
err = wc.Close()
if err != nil {
return err
}
return id
})
}
示例10: serveFilesGet
// Files
func serveFilesGet(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
serveAPI(res, req, func() interface{} {
ctx := appengine.NewContext(req)
session, _ := sessionStore.Get(req, "session")
_, ok := session.Values["email"].(string)
if !ok {
return HTTPError{403, "access denied"}
}
bucket, err := file.DefaultBucketName(ctx)
if err != nil {
return err
}
hc := &http.Client{
Transport: &oauth2.Transport{
Source: google.AppEngineTokenSource(ctx, storage.ScopeReadOnly),
Base: &urlfetch.Transport{Context: ctx},
},
}
cctx := cloud.NewContext(appengine.AppID(ctx), hc)
rc, err := storage.NewReader(cctx, bucket, params.ByName("id"))
if err != nil {
return err
}
name := req.URL.Query().Get("name")
if name == "" {
name = params.ByName("id")
}
name = regexp.MustCompile("[^a-zA-Z-_.]").ReplaceAllString(name, "")
res.Header().Set("Content-Disposition", "inline; filename=\""+name+"\"")
res.Header().Set("Content-Type", "application/octet-stream")
return rc
})
}
示例11: handler
func handler(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
http.NotFound(res, req)
return
}
ctx := appengine.NewContext(res)
if bucket == "" {
var err error
if bucket, err = file.DefaultBucketName(ctx); err != nil {
log.Errorf(ctx, "failed to get default GCS bucket name: %v", err)
return
}
}
//[END get_default_bucket]
//Make Client
client, err := storage.NewClient(ctx)
if err != nil {
log.Errorf(ctx, "failed to get default GCS bucket name: %v", err)
return
}
defer client.Close()
res.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(res, "Demo GCS Application running from Version: %v\n", appengine.VersionID(ctx))
fmt.Fprintf(res, "Using bucket name: %v\n\n", bucket)
d := &demo{
w: res,
ctx: ctx,
client: client,
bucket: client.Bucket(bucket),
}
n := "demo-testfile-go"
d.createFile(n)
d.listBucket()
}
示例12: Store
// Store binary data to GCS
func Store(c context.Context, data []byte, fileName, mimeType, bucketName string) (absFilename string, err error) {
if bucketName == "" {
var err error
if bucketName, err = file.DefaultBucketName(c); err != nil {
log.Errorf(c, "failed to get default GCS bucket name: %v", err)
return "", err
}
}
hc := &http.Client{
Transport: &oauth2.Transport{
Source: google.AppEngineTokenSource(c, storage.ScopeFullControl),
// Note that the App Engine urlfetch service has a limit of 10MB uploads and
// 32MB downloads.
// See https://cloud.google.com/appengine/docs/go/urlfetch/#Go_Quotas_and_limits
// for more information.
Base: &urlfetch.Transport{Context: c},
},
}
ctx := cloud.NewContext(appengine.AppID(c), hc)
wc := storage.NewWriter(ctx, bucketName, fileName)
wc.ContentType = mimeType
if _, err := wc.Write(data); err != nil {
log.Errorf(c, "upload file: unable to write data to bucket %q, file %q: %v", bucketName, fileName, err)
return "", err
}
if err := wc.Close(); err != nil {
log.Errorf(c, "upload file: unable to close bucket %q, file %q: %v", bucketName, fileName, err)
return "", err
}
return getAbsFilename(bucketName, fileName), nil
}
示例13: Reader
func Reader(filename string, r *http.Request) (io.ReadCloser, error) {
c := appengine.NewContext(r)
bucket, err := file.DefaultBucketName(c)
if err != nil {
log.Errorf(c, "Failed to get default bucket: %v", err)
return nil, err
}
ctx, err := auth(r)
if err != nil {
log.Errorf(c, "Failed to get context: %v", err)
return nil, err
}
log.Infof(c, "Retrieving file %v from bucket %v.", filename, bucket)
rc, err := storage.NewReader(ctx, bucket, filename)
if err != nil {
log.Errorf(c, "Failed to open file: %v", err)
return nil, err
}
return rc, nil
}
示例14: FileStats
func FileStats(filename string, r *http.Request) (*storage.Object, error) {
c := appengine.NewContext(r)
bucket, err := file.DefaultBucketName(c)
if err != nil {
log.Errorf(c, "Failed to get default bucket: %v", err)
return nil, err
}
ctx, err := auth(r)
if err != nil {
log.Errorf(c, "Failed to get context: %v", err)
return nil, err
}
log.Infof(c, "Getting stats for file %v from bucket %v.", filename, bucket)
obj, err := storage.StatObject(ctx, bucket, filename)
if err != nil {
log.Errorf(c, "Failed to stat file: %v", err)
return nil, err
}
return obj, nil
}
示例15: 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 {
//.........这里部分代码省略.........