本文整理匯總了Golang中cloud/google/com/go/storage.Client類的典型用法代碼示例。如果您正苦於以下問題:Golang Client類的具體用法?Golang Client怎麽用?Golang Client使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Client類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ExampleBucketHandle_List
func ExampleBucketHandle_List() {
ctx := context.Background()
var client *storage.Client // See Example (Auth)
var query *storage.Query
for {
// If you are using this package on the App Engine Flex runtime,
// you can init a bucket client with your app's default bucket name.
// See http://godoc.org/google.golang.org/appengine/file#DefaultBucketName.
objects, err := client.Bucket("bucketname").List(ctx, query)
if err != nil {
log.Fatal(err)
}
for _, obj := range objects.Results {
log.Printf("object name: %s, size: %v", obj.Name, obj.Size)
}
// If there are more results, objects.Next will be non-nil.
if objects.Next == nil {
break
}
query = objects.Next
}
log.Println("paginated through all object items in the bucket you specified.")
}
示例2: createOutputFile
func (s *shard) createOutputFile(c context.Context) (io.WriteCloser, error) {
c, _ = context.WithTimeout(c, time.Duration(10)*time.Minute)
// for development we can't use the appengine default credentials so
// instead need to create our own oauth token source to access storage
// TODO: maybe give job a chance to generate this - it could also
// create the writer (?). The only reason we're doing it is to prevent
// duplication and also handle the file rollup operations
var client *cstorage.Client
if appengine.IsDevAppServer() {
jsonKey, err := ioutil.ReadFile("service-account.json")
if err != nil {
return nil, err
}
conf, err := google.JWTConfigFromJSON(jsonKey, cstorage.ScopeReadWrite)
if err != nil {
return nil, err
}
client, err = cstorage.NewClient(c, option.WithTokenSource(conf.TokenSource(c)))
if err != nil {
return nil, err
}
} else {
var err error
client, err = cstorage.NewClient(c)
if err != nil {
return nil, err
}
}
o := client.Bucket(s.job.Bucket).Object(s.sliceFilename(s.Sequence)).NewWriter(c)
// TODO: wrap writer to count bytes and continue slice if we get close to 10Mb limit (?)
return o, nil
}
示例3: addObjectACL
func addObjectACL(client *storage.Client, bucket, object string) error {
ctx := context.Background()
acl := client.Bucket(bucket).Object(object).ACL()
if err := acl.Set(ctx, storage.AllAuthenticatedUsers, storage.RoleReader); err != nil {
return err
}
return nil
}
示例4: deleteDefaultBucketACL
func deleteDefaultBucketACL(client *storage.Client, bucket string) error {
ctx := context.Background()
acl := client.Bucket(bucket).DefaultObjectACL()
if err := acl.Delete(ctx, storage.AllAuthenticatedUsers); err != nil {
return err
}
return nil
}
示例5: delete
func delete(client *storage.Client, bucketName string) error {
ctx := context.Background()
// [START delete_bucket]
if err := client.Bucket(bucketName).Delete(ctx); err != nil {
return err
}
// [END delete_bucket]
return nil
}
示例6: create
func create(client *storage.Client, projectID, bucketName string) error {
ctx := context.Background()
// [START create_bucket]
if err := client.Bucket(bucketName).Create(ctx, projectID, nil); err != nil {
return err
}
// [END create_bucket]
return nil
}
示例7: makePublic
func makePublic(client *storage.Client, bucket, object string) error {
ctx := context.Background()
// [START public]
acl := client.Bucket(bucket).Object(object).ACL()
if err := acl.Set(ctx, storage.AllUsers, storage.RoleReader); err != nil {
return err
}
// [END public]
return nil
}
示例8: attrs
func attrs(client *storage.Client, bucket, object string) (*storage.ObjectAttrs, error) {
ctx := context.Background()
// [START get_metadata]
o := client.Bucket(bucket).Object(object)
attrs, err := o.Attrs(ctx)
if err != nil {
return nil, err
}
return attrs, nil
// [END get_metadata]
}
示例9: objectACL
func objectACL(client *storage.Client, bucket, object string) error {
ctx := context.Background()
rules, err := client.Bucket(bucket).Object(object).ACL().List(ctx)
if err != nil {
return err
}
for _, rule := range rules {
fmt.Printf("ACL rule: %v\n", rule)
}
return nil
}
示例10: copyToBucket
func copyToBucket(client *storage.Client, dstBucket, srcBucket, srcObject string) error {
ctx := context.Background()
// [START copy_file]
dstObject := srcObject + "-copy"
src := client.Bucket(srcBucket).Object(srcObject)
dst := client.Bucket(dstBucket).Object(dstObject)
if _, err := dst.CopierFrom(src).Run(ctx); err != nil {
return err
}
// [END copy_file]
return nil
}
示例11: createWithAttrs
func createWithAttrs(client *storage.Client, projectID, bucketName string) error {
ctx := context.Background()
// [START create_bucket_with_storageclass_and_location]
bucket := client.Bucket(bucketName)
if err := bucket.Create(ctx, projectID, &storage.BucketAttrs{
StorageClass: "COLDLINE",
Location: "asia",
}); err != nil {
return err
}
// [END create_bucket_with_storageclass_and_location]
return nil
}
示例12: objectACLFiltered
func objectACLFiltered(client *storage.Client, bucket, object string, entity storage.ACLEntity) error {
ctx := context.Background()
rules, err := client.Bucket(bucket).ACL().List(ctx)
if err != nil {
return err
}
for _, r := range rules {
if r.Entity == entity {
fmt.Printf("ACL rule role: %v\n", r.Role)
}
}
return nil
}
示例13: GetObject
// Gets a single object's bytes based on bucket and name parameters
func GetObject(gc *storage.Client, bucket, name string) (*bytes.Buffer, error) {
rc, err := gc.Bucket(bucket).Object(name).NewReader(context.Background())
if err != nil {
if err == storage.ErrObjectNotExist {
return nil, cloudstorage.ObjectNotFound
}
return nil, err
}
by, err := ioutil.ReadAll(rc)
if err != nil {
return nil, err
}
return bytes.NewBuffer(by), nil
}
示例14: writeEncryptedObject
func writeEncryptedObject(client *storage.Client, bucket, object string, secretKey []byte) error {
ctx := context.Background()
// [START storage_upload_encrypted_file]
obj := client.Bucket(bucket).Object(object)
// Encrypt the object's contents.
wc := obj.Key(secretKey).NewWriter(ctx)
if _, err := wc.Write([]byte("top secret")); err != nil {
return err
}
if err := wc.Close(); err != nil {
return err
}
// [END storage_upload_encrypted_file]
return nil
}
示例15: read
func read(client *storage.Client, bucket, object string) ([]byte, error) {
ctx := context.Background()
// [START download_file]
rc, err := client.Bucket(bucket).Object(object).NewReader(ctx)
if err != nil {
return nil, err
}
defer rc.Close()
data, err := ioutil.ReadAll(rc)
if err != nil {
return nil, err
}
return data, nil
// [END download_file]
}