/usr/share/gocode/src/github.com/jacobsa/gcloud/gcs/bucket.go is in golang-github-jacobsa-gcloud-dev 0.0~git20150709-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | // Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gcs
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/jacobsa/gcloud/httputil"
"golang.org/x/net/context"
"google.golang.org/api/googleapi"
storagev1 "google.golang.org/api/storage/v1"
)
// Bucket represents a GCS bucket, pre-bound with a bucket name and necessary
// authorization information.
//
// Each method that may block accepts a context object that is used for
// deadlines and cancellation. Users need not package authorization information
// into the context object (using cloud.WithContext or similar).
//
// All methods are safe for concurrent access.
type Bucket interface {
Name() string
// Create a reader for the contents of a particular generation of an object.
// On a nil error, the caller must arrange for the reader to be closed when
// it is no longer needed.
//
// Non-existent objects cause either this method or the first read from the
// resulting reader to return an error of type *NotFoundError.
//
// Official documentation:
// https://cloud.google.com/storage/docs/json_api/v1/objects/get
NewReader(
ctx context.Context,
req *ReadObjectRequest) (io.ReadCloser, error)
// Create or overwrite an object according to the supplied request. The new
// object is guaranteed to exist immediately for the purposes of reading (and
// eventually for listing) after this method returns a nil error. It is
// guaranteed not to exist before req.Contents returns io.EOF.
//
// If the request fails due to a precondition not being met, the error will
// be of type *PreconditionError.
//
// Official documentation:
// https://cloud.google.com/storage/docs/json_api/v1/objects/insert
// https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload
CreateObject(
ctx context.Context,
req *CreateObjectRequest) (*Object, error)
// Copy an object to a new name, preserving all metadata. Any existing
// generation of the destination name will be overwritten.
//
// Returns a record for the new object.
//
// If the source object doesn't exist, err will be of type *NotFoundError.
//
// Official documentation:
// https://cloud.google.com/storage/docs/json_api/v1/objects/copy
CopyObject(
ctx context.Context,
req *CopyObjectRequest) (*Object, error)
// Compose zero or more source objects into a single destination object by
// concatenating. Any existing generation of the destination name will be
// overwritten.
//
// Returns a record for the new object.
//
// If any of the sources don't exist, err will be of type *NotFoundError. If
// the request fails due to a precondition not being met, the error will be
// of type *PreconditionError.
//
// Official documentation:
// https://cloud.google.com/storage/docs/json_api/v1/objects/compose
ComposeObjects(
ctx context.Context,
req *ComposeObjectsRequest) (*Object, error)
// Return current information about the object with the given name.
//
// If the object doesn't exist, err will be of type *NotFoundError.
//
// Official documentation:
// https://cloud.google.com/storage/docs/json_api/v1/objects/get
StatObject(
ctx context.Context,
req *StatObjectRequest) (*Object, error)
// List the objects in the bucket that meet the criteria defined by the
// request, returning a result object that contains the results and
// potentially a cursor for retrieving the next portion of the larger set of
// results.
//
// Official documentation:
// https://cloud.google.com/storage/docs/json_api/v1/objects/list
ListObjects(
ctx context.Context,
req *ListObjectsRequest) (*Listing, error)
// Update the object specified by newAttrs.Name, patching using the non-zero
// fields of newAttrs.
//
// If the object doesn't exist, err will be of type *NotFoundError.
//
// Official documentation:
// https://cloud.google.com/storage/docs/json_api/v1/objects/patch
UpdateObject(
ctx context.Context,
req *UpdateObjectRequest) (*Object, error)
// Delete an object. Non-existence of the object is not treated as an error.
//
// Official documentation:
// https://cloud.google.com/storage/docs/json_api/v1/objects/delete
DeleteObject(
ctx context.Context,
req *DeleteObjectRequest) error
}
type bucket struct {
client *http.Client
userAgent string
name string
}
func (b *bucket) Name() string {
return b.name
}
func (b *bucket) ListObjects(
ctx context.Context,
req *ListObjectsRequest) (listing *Listing, err error) {
// Construct an appropriate URL (cf. http://goo.gl/aVSAhT).
opaque := fmt.Sprintf(
"//www.googleapis.com/storage/v1/b/%s/o",
httputil.EncodePathSegment(b.Name()))
query := make(url.Values)
query.Set("projection", "full")
if req.Prefix != "" {
query.Set("prefix", req.Prefix)
}
if req.Delimiter != "" {
query.Set("delimiter", req.Delimiter)
}
if req.ContinuationToken != "" {
query.Set("pageToken", req.ContinuationToken)
}
if req.MaxResults != 0 {
query.Set("maxResults", fmt.Sprintf("%v", req.MaxResults))
}
url := &url.URL{
Scheme: "https",
Host: "www.googleapis.com",
Opaque: opaque,
RawQuery: query.Encode(),
}
// Create an HTTP request.
httpReq, err := httputil.NewRequest("GET", url, nil, b.userAgent)
if err != nil {
err = fmt.Errorf("httputil.NewRequest: %v", err)
return
}
// Call the server.
httpRes, err := httputil.Do(ctx, b.client, httpReq)
if err != nil {
return
}
defer googleapi.CloseBody(httpRes)
// Check for HTTP-level errors.
if err = googleapi.CheckResponse(httpRes); err != nil {
return
}
// Parse the response.
var rawListing *storagev1.Objects
if err = json.NewDecoder(httpRes.Body).Decode(&rawListing); err != nil {
return
}
// Convert the response.
if listing, err = toListing(rawListing); err != nil {
return
}
return
}
func (b *bucket) StatObject(
ctx context.Context,
req *StatObjectRequest) (o *Object, err error) {
// Construct an appropriate URL (cf. http://goo.gl/MoITmB).
opaque := fmt.Sprintf(
"//www.googleapis.com/storage/v1/b/%s/o/%s",
httputil.EncodePathSegment(b.Name()),
httputil.EncodePathSegment(req.Name))
query := make(url.Values)
query.Set("projection", "full")
url := &url.URL{
Scheme: "https",
Host: "www.googleapis.com",
Opaque: opaque,
RawQuery: query.Encode(),
}
// Create an HTTP request.
httpReq, err := httputil.NewRequest("GET", url, nil, b.userAgent)
if err != nil {
err = fmt.Errorf("httputil.NewRequest: %v", err)
return
}
// Execute the HTTP request.
httpRes, err := httputil.Do(ctx, b.client, httpReq)
if err != nil {
return
}
defer googleapi.CloseBody(httpRes)
// Check for HTTP-level errors.
if err = googleapi.CheckResponse(httpRes); err != nil {
// Special case: handle not found errors.
if typed, ok := err.(*googleapi.Error); ok {
if typed.Code == http.StatusNotFound {
err = &NotFoundError{Err: typed}
}
}
return
}
// Parse the response.
var rawObject *storagev1.Object
if err = json.NewDecoder(httpRes.Body).Decode(&rawObject); err != nil {
return
}
// Convert the response.
if o, err = toObject(rawObject); err != nil {
err = fmt.Errorf("toObject: %v", err)
return
}
return
}
func (b *bucket) DeleteObject(
ctx context.Context,
req *DeleteObjectRequest) (err error) {
// Construct an appropriate URL (cf. http://goo.gl/TRQJjZ).
opaque := fmt.Sprintf(
"//www.googleapis.com/storage/v1/b/%s/o/%s",
httputil.EncodePathSegment(b.Name()),
httputil.EncodePathSegment(req.Name))
query := make(url.Values)
if req.Generation != 0 {
query.Set("generation", fmt.Sprintf("%d", req.Generation))
}
url := &url.URL{
Scheme: "https",
Host: "www.googleapis.com",
Opaque: opaque,
RawQuery: query.Encode(),
}
// Create an HTTP request.
httpReq, err := httputil.NewRequest("DELETE", url, nil, b.userAgent)
if err != nil {
err = fmt.Errorf("httputil.NewRequest: %v", err)
return
}
// Execute the HTTP request.
httpRes, err := httputil.Do(ctx, b.client, httpReq)
if err != nil {
return
}
defer googleapi.CloseBody(httpRes)
// Check for HTTP-level errors.
err = googleapi.CheckResponse(httpRes)
// Special case: we want deletes to be idempotent.
if typed, ok := err.(*googleapi.Error); ok {
if typed.Code == http.StatusNotFound {
err = nil
}
}
// Propagate other errors.
if err != nil {
return
}
return
}
func newBucket(
client *http.Client,
userAgent string,
name string) Bucket {
return &bucket{
client: client,
userAgent: userAgent,
name: name,
}
}
|