Documentation ¶
Overview ¶
Package storage contains a Google Cloud Storage client.
This package is experimental and may make backwards-incompatible changes.
Example (Auth) ¶
package main import ( "io/ioutil" "log" "golang.org/x/net/context" "golang.org/x/oauth2/google" "google.golang.org/cloud" "google.golang.org/cloud/storage" ) func main() { // Initialize an authorized context with Google Developers Console // JSON key. Read the google package examples to learn more about // different authorization flows you can use. // http://godoc.org/golang.org/x/oauth2/google jsonKey, err := ioutil.ReadFile("/path/to/json/keyfile.json") if err != nil { log.Fatal(err) } conf, err := google.JWTConfigFromJSON( jsonKey, storage.ScopeFullControl, ) if err != nil { log.Fatal(err) } ctx := context.Background() client, err := storage.NewClient(ctx, cloud.WithTokenSource(conf.TokenSource(ctx))) if err != nil { log.Fatal(err) } // Use the client (see other examples) doSomething(client) // After using the client, free any resources (e.g. network connections). client.Close() } func doSomething(c *storage.Client) {}
Output:
Index ¶
- Constants
- Variables
- func SignedURL(bucket, name string, opts *SignedURLOptions) (string, error)
- type ACLEntity
- type ACLHandle
- type ACLRole
- type ACLRule
- type AdminClient
- type BucketAttrs
- type BucketHandle
- func (c *BucketHandle) ACL() *ACLHandle
- func (b *BucketHandle) Attrs(ctx context.Context) (*BucketAttrs, error)
- func (c *BucketHandle) DefaultObjectACL() *ACLHandle
- func (b *BucketHandle) List(ctx context.Context, q *Query) (*ObjectList, error)
- func (b *BucketHandle) Object(name string) *ObjectHandle
- type Client
- type Condition
- type ObjectAttrs
- type ObjectHandle
- func (o *ObjectHandle) ACL() *ACLHandle
- func (o *ObjectHandle) Attrs(ctx context.Context) (*ObjectAttrs, error)
- func (o *ObjectHandle) CopyTo(ctx context.Context, dst *ObjectHandle, attrs *ObjectAttrs) (*ObjectAttrs, error)
- func (o *ObjectHandle) Delete(ctx context.Context) error
- func (o *ObjectHandle) NewRangeReader(ctx context.Context, offset, length int64) (*Reader, error)
- func (o *ObjectHandle) NewReader(ctx context.Context) (*Reader, error)
- func (o *ObjectHandle) NewWriter(ctx context.Context) *Writer
- func (o *ObjectHandle) Update(ctx context.Context, attrs ObjectAttrs) (*ObjectAttrs, error)
- func (o *ObjectHandle) WithConditions(conds ...Condition) *ObjectHandle
- type ObjectList
- type Query
- type Reader
- type SignedURLOptions
- type Writer
Examples ¶
Constants ¶
const ( // ScopeFullControl grants permissions to manage your // data and permissions in Google Cloud Storage. ScopeFullControl = raw.DevstorageFullControlScope // ScopeReadOnly grants permissions to // view your data in Google Cloud Storage. ScopeReadOnly = raw.DevstorageReadOnlyScope // ScopeReadWrite grants permissions to manage your // data in Google Cloud Storage. ScopeReadWrite = raw.DevstorageReadWriteScope )
Variables ¶
var ( ErrBucketNotExist = errors.New("storage: bucket doesn't exist") ErrObjectNotExist = errors.New("storage: object doesn't exist") )
Functions ¶
func SignedURL ¶
func SignedURL(bucket, name string, opts *SignedURLOptions) (string, error)
SignedURL returns a URL for the specified object. Signed URLs allow the users access to a restricted resource for a limited time without having a Google account or signing in. For more information about the signed URLs, see https://cloud.google.com/storage/docs/accesscontrol#Signed-URLs.
Types ¶
type ACLEntity ¶
type ACLEntity string
ACLEntity refers to a user or group. They are sometimes referred to as grantees.
It could be in the form of: "user-<userId>", "user-<email>", "group-<groupId>", "group-<email>", "domain-<domain>" and "project-team-<projectId>".
Or one of the predefined constants: AllUsers, AllAuthenticatedUsers.
type ACLHandle ¶
type ACLHandle struct {
// contains filtered or unexported fields
}
ACLHandle provides operations on an access control list for a Google Cloud Storage bucket or object.
type ACLRule ¶
ACLRule represents a grant for a role to an entity (user, group or team) for a Google Cloud Storage object or bucket.
type AdminClient ¶
type AdminClient struct {
// contains filtered or unexported fields
}
AdminClient is a client type for performing admin operations on a project's buckets.
func NewAdminClient ¶
func NewAdminClient(ctx context.Context, projectID string, opts ...cloud.ClientOption) (*AdminClient, error)
NewAdminClient creates a new AdminClient for a given project.
func (*AdminClient) CreateBucket ¶
func (c *AdminClient) CreateBucket(ctx context.Context, bucketName string, attrs *BucketAttrs) error
Create creates a Bucket in the project. If attrs is nil the API defaults will be used.
func (*AdminClient) DeleteBucket ¶
func (c *AdminClient) DeleteBucket(ctx context.Context, bucketName string) error
Delete deletes a Bucket in the project.
type BucketAttrs ¶
type BucketAttrs struct { // Name is the name of the bucket. Name string // ACL is the list of access control rules on the bucket. ACL []ACLRule // DefaultObjectACL is the list of access controls to // apply to new objects when no object ACL is provided. DefaultObjectACL []ACLRule // Location is the location of the bucket. It defaults to "US". Location string // MetaGeneration is the metadata generation of the bucket. MetaGeneration int64 // StorageClass is the storage class of the bucket. This defines // how objects in the bucket are stored and determines the SLA // and the cost of storage. Typical values are "STANDARD" and // "DURABLE_REDUCED_AVAILABILITY". Defaults to "STANDARD". StorageClass string // Created is the creation time of the bucket. Created time.Time }
BucketAttrs represents the metadata for a Google Cloud Storage bucket.
type BucketHandle ¶
type BucketHandle struct {
// contains filtered or unexported fields
}
BucketHandle provides operations on a Google Cloud Storage bucket. Use Client.Bucket to get a handle.
func (*BucketHandle) ACL ¶
func (c *BucketHandle) ACL() *ACLHandle
ACL returns an ACLHandle, which provides access to the bucket's access control list. This controls who can list, create or overwrite the objects in a bucket. This call does not perform any network operations.
func (*BucketHandle) Attrs ¶
func (b *BucketHandle) Attrs(ctx context.Context) (*BucketAttrs, error)
Attrs returns the metadata for the bucket.
func (*BucketHandle) DefaultObjectACL ¶
func (c *BucketHandle) DefaultObjectACL() *ACLHandle
DefaultObjectACL returns an ACLHandle, which provides access to the bucket's default object ACLs. These ACLs are applied to newly created objects in this bucket that do not have a defined ACL. This call does not perform any network operations.
func (*BucketHandle) List ¶
func (b *BucketHandle) List(ctx context.Context, q *Query) (*ObjectList, error)
List lists objects from the bucket. You can specify a query to filter the results. If q is nil, no filtering is applied.
func (*BucketHandle) Object ¶
func (b *BucketHandle) Object(name string) *ObjectHandle
Object returns an ObjectHandle, which provides operations on the named object. This call does not perform any network operations.
name must consist entirely of valid UTF-8-encoded runes. The full specification for valid object names can be found at:
https://cloud.google.com/storage/docs/bucket-naming
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a client for interacting with Google Cloud Storage.
func NewClient ¶
NewClient creates a new Google Cloud Storage client. The default scope is ScopeFullControl. To use a different scope, like ScopeReadOnly, use cloud.WithScopes.
func (*Client) Bucket ¶
func (c *Client) Bucket(name string) *BucketHandle
Bucket returns a BucketHandle, which provides operations on the named bucket. This call does not perform any network operations.
name must contain only lowercase letters, numbers, dashes, underscores, and dots. The full specification for valid bucket names can be found at:
https://cloud.google.com/storage/docs/bucket-naming
type Condition ¶
type Condition interface {
// contains filtered or unexported methods
}
A Condition constrains methods to act on specific generations of resources.
Not all conditions or combinations of conditions are applicable to all methods.
func Generation ¶
func IfGenerationMatch ¶
func IfGenerationNotMatch ¶
func IfMetaGenerationMatch ¶
type ObjectAttrs ¶
type ObjectAttrs struct { // Bucket is the name of the bucket containing this GCS object. // This field is read-only. Bucket string // Name is the name of the object within the bucket. // This field is read-only. Name string // ContentType is the MIME type of the object's content. ContentType string // ContentLanguage is the content language of the object's content. ContentLanguage string // CacheControl is the Cache-Control header to be sent in the response // headers when serving the object data. CacheControl string // ACL is the list of access control rules for the object. ACL []ACLRule // Owner is the owner of the object. This field is read-only. // // If non-zero, it is in the form of "user-<userId>". Owner string // Size is the length of the object's content. This field is read-only. Size int64 // ContentEncoding is the encoding of the object's content. ContentEncoding string // ContentDisposition is the optional Content-Disposition header of the object // sent in the response headers. ContentDisposition string // MD5 is the MD5 hash of the object's content. This field is read-only. MD5 []byte // CRC32C is the CRC32 checksum of the object's content using // the Castagnoli93 polynomial. This field is read-only. CRC32C uint32 // MediaLink is an URL to the object's content. This field is read-only. MediaLink string // Metadata represents user-provided metadata, in key/value pairs. // It can be nil if no metadata is provided. Metadata map[string]string // Generation is the generation number of the object's content. // This field is read-only. Generation int64 // MetaGeneration is the version of the metadata for this // object at this generation. This field is used for preconditions // and for detecting changes in metadata. A metageneration number // is only meaningful in the context of a particular generation // of a particular object. This field is read-only. MetaGeneration int64 // StorageClass is the storage class of the bucket. // This value defines how objects in the bucket are stored and // determines the SLA and the cost of storage. Typical values are // "STANDARD" and "DURABLE_REDUCED_AVAILABILITY". // It defaults to "STANDARD". This field is read-only. StorageClass string // Created is the time the object was created. This field is read-only. Created time.Time // Deleted is the time the object was deleted. // If not deleted, it is the zero value. This field is read-only. Deleted time.Time // Updated is the creation or modification time of the object. // For buckets with versioning enabled, changing an object's // metadata does not change this property. This field is read-only. Updated time.Time }
ObjectAttrs represents the metadata for a Google Cloud Storage (GCS) object.
type ObjectHandle ¶
type ObjectHandle struct {
// contains filtered or unexported fields
}
ObjectHandle provides operations on an object in a Google Cloud Storage bucket. Use BucketHandle.Object to get a handle.
func (*ObjectHandle) ACL ¶
func (o *ObjectHandle) ACL() *ACLHandle
ACL provides access to the object's access control list. This controls who can read and write this object. This call does not perform any network operations.
func (*ObjectHandle) Attrs ¶
func (o *ObjectHandle) Attrs(ctx context.Context) (*ObjectAttrs, error)
Attrs returns meta information about the object. ErrObjectNotExist will be returned if the object is not found.
func (*ObjectHandle) CopyTo ¶
func (o *ObjectHandle) CopyTo(ctx context.Context, dst *ObjectHandle, attrs *ObjectAttrs) (*ObjectAttrs, error)
CopyTo copies the object to the given dst. The copied object's attributes are overwritten by attrs if non-nil.
Example ¶
package main import ( "log" "golang.org/x/net/context" "google.golang.org/cloud/storage" ) func main() { ctx := context.Background() var client *storage.Client // See Example (Auth) src := client.Bucket("bucketname").Object("file1") dst := client.Bucket("another-bucketname").Object("file2") o, err := src.CopyTo(ctx, dst, nil) if err != nil { log.Fatal(err) } log.Println("copied file:", o) }
Output:
func (*ObjectHandle) Delete ¶
func (o *ObjectHandle) Delete(ctx context.Context) error
Delete deletes the single specified object.
func (*ObjectHandle) NewRangeReader ¶
NewRangeReader reads part of an object, reading at most length bytes starting at the given offset. If length is negative, the object is read until the end.
func (*ObjectHandle) NewReader ¶
func (o *ObjectHandle) NewReader(ctx context.Context) (*Reader, error)
NewReader creates a new Reader to read the contents of the object. ErrObjectNotExist will be returned if the object is not found.
func (*ObjectHandle) NewWriter ¶
func (o *ObjectHandle) NewWriter(ctx context.Context) *Writer
NewWriter returns a storage Writer that writes to the GCS object associated with this ObjectHandle.
A new object will be created if an object with this name already exists. Otherwise any previous object with the same name will be replaced. The object will not be available (and any previous object will remain) until Close has been called.
Attributes can be set on the object by modifying the returned Writer's ObjectAttrs field before the first call to Write. If no ContentType attribute is specified, the content type will be automatically sniffed using net/http.DetectContentType.
It is the caller's responsibility to call Close when writing is done.
func (*ObjectHandle) Update ¶
func (o *ObjectHandle) Update(ctx context.Context, attrs ObjectAttrs) (*ObjectAttrs, error)
Update updates an object with the provided attributes. All zero-value attributes are ignored. ErrObjectNotExist will be returned if the object is not found.
func (*ObjectHandle) WithConditions ¶
func (o *ObjectHandle) WithConditions(conds ...Condition) *ObjectHandle
WithConditions returns a copy of o using the provided conditions.
type ObjectList ¶
type ObjectList struct { // Results represent a list of object results. Results []*ObjectAttrs // Next is the continuation query to retrieve more // results with the same filtering criteria. If there // are no more results to retrieve, it is nil. Next *Query // Prefixes represents prefixes of objects // matching-but-not-listed up to and including // the requested delimiter. Prefixes []string }
ObjectList represents a list of objects returned from a bucket List call.
type Query ¶
type Query struct { // Delimiter returns results in a directory-like fashion. // Results will contain only objects whose names, aside from the // prefix, do not contain delimiter. Objects whose names, // aside from the prefix, contain delimiter will have their name, // truncated after the delimiter, returned in prefixes. // Duplicate prefixes are omitted. // Optional. Delimiter string // Prefix is the prefix filter to query objects // whose names begin with this prefix. // Optional. Prefix string // Versions indicates whether multiple versions of the same // object will be included in the results. Versions bool // Cursor is a previously-returned page token // representing part of the larger set of results to view. // Optional. Cursor string // MaxResults is the maximum number of items plus prefixes // to return. As duplicate prefixes are omitted, // fewer total results may be returned than requested. // The default page limit is used if it is negative or zero. MaxResults int }
Query represents a query to filter objects from a bucket.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reads a Cloud Storage object.
func (*Reader) ContentType ¶
ContentType returns the content type of the object.
type SignedURLOptions ¶
type SignedURLOptions struct { // GoogleAccessID represents the authorizer of the signed URL generation. // It is typically the Google service account client email address from // the Google Developers Console in the form of "xxx@developer.gserviceaccount.com". // Required. GoogleAccessID string // PrivateKey is the Google service account private key. It is obtainable // from the Google Developers Console. // At https://console.developers.google.com/project/<your-project-id>/apiui/credential, // create a service account client ID or reuse one of your existing service account // credentials. Click on the "Generate new P12 key" to generate and download // a new private key. Once you download the P12 file, use the following command // to convert it into a PEM file. // // $ openssl pkcs12 -in key.p12 -passin pass:notasecret -out key.pem -nodes // // Provide the contents of the PEM file as a byte slice. // Required. PrivateKey []byte // Method is the HTTP method to be used with the signed URL. // Signed URLs can be used with GET, HEAD, PUT, and DELETE requests. // Required. Method string // Expires is the expiration time on the signed URL. It must be // a datetime in the future. // Required. Expires time.Time // ContentType is the content type header the client must provide // to use the generated signed URL. // Optional. ContentType string // Headers is a list of extention headers the client must provide // in order to use the generated signed URL. // Optional. Headers []string // MD5 is the base64 encoded MD5 checksum of the file. // If provided, the client should provide the exact value on the request // header in order to use the signed URL. // Optional. MD5 []byte }
SignedURLOptions allows you to restrict the access to the signed URL.
type Writer ¶
type Writer struct { // ObjectAttrs are optional attributes to set on the object. Any attributes // must be initialized before the first Write call. Nil or zero-valued // attributes are ignored. ObjectAttrs // contains filtered or unexported fields }
A Writer writes a Cloud Storage object.
func (*Writer) Attrs ¶
func (w *Writer) Attrs() *ObjectAttrs
ObjectAttrs returns metadata about a successfully-written object. It's only valid to call it after Close returns nil.
func (*Writer) Close ¶
Close completes the write operation and flushes any buffered data. If Close doesn't return an error, metadata about the written object can be retrieved by calling Object.
func (*Writer) CloseWithError ¶
CloseWithError aborts the write operation with the provided error. CloseWithError always returns nil.