2011-07-28 17:26:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
// Package googlestorage is simple Google Cloud Storage client.
|
|
|
|
//
|
|
|
|
// It does not include any Camlistore-specific logic.
|
2011-07-28 17:26:39 +00:00
|
|
|
package googlestorage
|
|
|
|
|
|
|
|
import (
|
2014-08-16 05:27:15 +00:00
|
|
|
"bytes"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"encoding/xml"
|
2014-08-03 02:51:41 +00:00
|
|
|
"errors"
|
2011-07-28 17:26:39 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-08-16 05:27:15 +00:00
|
|
|
"io/ioutil"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2014-08-05 19:45:10 +00:00
|
|
|
"os"
|
2011-07-28 17:26:39 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-08-16 05:27:15 +00:00
|
|
|
"unicode/utf8"
|
2011-07-28 17:26:39 +00:00
|
|
|
|
2014-01-29 06:00:52 +00:00
|
|
|
"camlistore.org/pkg/httputil"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/third_party/code.google.com/p/goauth2/oauth"
|
2014-08-03 04:00:43 +00:00
|
|
|
api "camlistore.org/third_party/code.google.com/p/google-api-go-client/storage/v1"
|
2014-08-03 02:29:57 +00:00
|
|
|
"camlistore.org/third_party/github.com/bradfitz/gce"
|
2011-07-28 17:26:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2013-07-06 20:29:17 +00:00
|
|
|
gsAccessURL = "https://storage.googleapis.com"
|
2011-07-28 17:26:39 +00:00
|
|
|
)
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
// A Client provides access to Google Cloud Storage.
|
2011-07-28 17:26:39 +00:00
|
|
|
type Client struct {
|
|
|
|
client *http.Client
|
2014-08-03 02:29:57 +00:00
|
|
|
transport *oauth.Transport // nil for service clients
|
2014-08-03 04:00:43 +00:00
|
|
|
service *api.Service
|
2011-07-28 17:26:39 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
// An Object holds the name of an object (its bucket and key) within
|
|
|
|
// Google Cloud Storage.
|
2011-07-28 17:26:39 +00:00
|
|
|
type Object struct {
|
|
|
|
Bucket string
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
func (o *Object) valid() error {
|
|
|
|
if o == nil {
|
|
|
|
return errors.New("invalid nil Object")
|
|
|
|
}
|
|
|
|
if o.Bucket == "" {
|
|
|
|
return errors.New("missing required Bucket field in Object")
|
|
|
|
}
|
|
|
|
if o.Key == "" {
|
|
|
|
return errors.New("missing required Key field in Object")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A SizedObject holds the bucket, key, and size of an object.
|
2011-07-28 17:26:39 +00:00
|
|
|
type SizedObject struct {
|
|
|
|
Object
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
2014-08-03 02:29:57 +00:00
|
|
|
// NewServiceClient returns a Client for use when running on Google
|
|
|
|
// Compute Engine. This client can access buckets owned by the samre
|
|
|
|
// project ID as the VM.
|
2014-08-03 02:51:41 +00:00
|
|
|
func NewServiceClient() (*Client, error) {
|
|
|
|
if !gce.OnGCE() {
|
|
|
|
return nil, errors.New("not running on Google Compute Engine")
|
|
|
|
}
|
|
|
|
scopes, _ := gce.Scopes("default")
|
|
|
|
if !scopes.Contains("https://www.googleapis.com/auth/devstorage.full_control") &&
|
|
|
|
!scopes.Contains("https://www.googleapis.com/auth/devstorage.read_write") {
|
|
|
|
return nil, errors.New("when this Google Compute Engine VM instance was created, it wasn't granted access to Cloud Storage")
|
|
|
|
}
|
2014-08-03 04:00:43 +00:00
|
|
|
service, _ := api.New(gce.Client)
|
|
|
|
return &Client{client: gce.Client, service: service}, nil
|
2014-08-03 02:29:57 +00:00
|
|
|
}
|
|
|
|
|
2011-07-28 17:26:39 +00:00
|
|
|
func NewClient(transport *oauth.Transport) *Client {
|
2014-08-03 04:00:43 +00:00
|
|
|
client := transport.Client()
|
|
|
|
service, _ := api.New(client)
|
|
|
|
return &Client{
|
|
|
|
client: transport.Client(),
|
|
|
|
transport: transport,
|
|
|
|
service: service,
|
|
|
|
}
|
2011-07-28 17:26:39 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
func (o *Object) String() string {
|
|
|
|
if o == nil {
|
|
|
|
return "<nil *Object>"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v/%v", o.Bucket, o.Key)
|
2011-07-28 17:26:39 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
func (so SizedObject) String() string {
|
|
|
|
return fmt.Sprintf("%v/%v (%vB)", so.Bucket, so.Key, so.Size)
|
2011-07-28 17:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A close relative to http.Client.Do(), helping with token refresh logic.
|
2013-07-06 20:29:17 +00:00
|
|
|
// If canResend is true and the initial request's response is an auth error
|
|
|
|
// (401 or 403), oauth credentials will be refreshed and the request sent
|
2011-07-28 17:26:39 +00:00
|
|
|
// again. This should only be done for requests with empty bodies, since the
|
|
|
|
// Body will be consumed on the first attempt if it exists.
|
2013-07-06 20:29:17 +00:00
|
|
|
// If canResend is false, and req would have been resent if canResend were
|
2011-07-28 17:26:39 +00:00
|
|
|
// true, then shouldRetry will be true.
|
|
|
|
// One of resp or err will always be nil.
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (gsa *Client) doRequest(req *http.Request, canResend bool) (resp *http.Response, err error, shouldRetry bool) {
|
2014-08-03 02:29:57 +00:00
|
|
|
resp, err = gsa.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if gsa.transport == nil {
|
2011-07-28 17:26:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == 401 || resp.StatusCode == 403 {
|
|
|
|
// Unauth. Perhaps tokens need refreshing?
|
|
|
|
if err = gsa.transport.Refresh(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Refresh succeeded. req should be resent
|
|
|
|
if !canResend {
|
|
|
|
return resp, nil, true
|
|
|
|
}
|
|
|
|
// Resend req. First, need to close the soon-overwritten response Body
|
|
|
|
resp.Body.Close()
|
|
|
|
resp, err = gsa.client.Do(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Makes a simple body-less google storage request
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (gsa *Client) simpleRequest(method, url_ string) (resp *http.Response, err error) {
|
2011-07-28 17:26:39 +00:00
|
|
|
// Construct the request
|
2011-08-25 15:14:47 +00:00
|
|
|
req, err := http.NewRequest(method, url_, nil)
|
2011-07-28 17:26:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("x-goog-api-version", "2")
|
|
|
|
|
|
|
|
resp, err, _ = gsa.doRequest(req, true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
// GetObject fetches a Google Cloud Storage object.
|
|
|
|
// The caller must close rc.
|
2014-10-11 14:02:31 +00:00
|
|
|
func (c *Client) GetObject(obj *Object) (rc io.ReadCloser, size int64, err error) {
|
2014-08-08 18:03:10 +00:00
|
|
|
if err = obj.valid(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-10-11 14:02:31 +00:00
|
|
|
resp, err := c.simpleRequest("GET", gsAccessURL+"/"+obj.Bucket+"/"+obj.Key)
|
2011-07-28 17:26:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("GS GET request failed: %v\n", err)
|
|
|
|
}
|
2014-08-05 19:45:10 +00:00
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
2014-10-11 14:02:31 +00:00
|
|
|
resp.Body.Close()
|
2014-08-05 19:45:10 +00:00
|
|
|
return nil, 0, os.ErrNotExist
|
|
|
|
}
|
2011-07-28 17:26:39 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2014-10-11 14:02:31 +00:00
|
|
|
resp.Body.Close()
|
2011-07-28 17:26:39 +00:00
|
|
|
return nil, 0, fmt.Errorf("GS GET request failed status: %v\n", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Body, resp.ContentLength, nil
|
|
|
|
}
|
|
|
|
|
2014-10-11 14:02:31 +00:00
|
|
|
// GetPartialObject fetches part of a Google Cloud Storage object.
|
|
|
|
// If length is negative, the rest of the object is returned.
|
|
|
|
// The caller must close rc.
|
|
|
|
func (c *Client) GetPartialObject(obj Object, offset, length int64) (rc io.ReadCloser, err error) {
|
|
|
|
if offset < 0 {
|
|
|
|
return nil, errors.New("invalid negative length")
|
|
|
|
}
|
|
|
|
if err = obj.valid(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", gsAccessURL+"/"+obj.Bucket+"/"+obj.Key, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("x-goog-api-version", "2")
|
|
|
|
if length >= 0 {
|
|
|
|
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+length-1))
|
|
|
|
} else {
|
|
|
|
req.Header.Set("Range", fmt.Sprintf("bytes=%d-", offset))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err, _ := c.doRequest(req, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("GS GET request failed: %v\n", err)
|
|
|
|
}
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil, os.ErrNotExist
|
|
|
|
}
|
|
|
|
if !(resp.StatusCode == http.StatusPartialContent || (offset == 0 && resp.StatusCode == http.StatusOK)) {
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil, fmt.Errorf("GS GET request failed status: %v\n", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Body, nil
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
// StatObject checks for the size & existence of a Google Cloud Storage object.
|
|
|
|
// Non-existence of a file is not an error.
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (gsa *Client) StatObject(obj *Object) (size int64, exists bool, err error) {
|
2014-08-08 18:03:10 +00:00
|
|
|
if err = obj.valid(); err != nil {
|
2011-07-28 17:26:39 +00:00
|
|
|
return
|
|
|
|
}
|
2014-08-08 18:03:10 +00:00
|
|
|
res, err := gsa.simpleRequest("HEAD", gsAccessURL+"/"+obj.Bucket+"/"+obj.Key)
|
|
|
|
if err != nil {
|
2011-07-28 17:26:39 +00:00
|
|
|
return
|
|
|
|
}
|
2014-08-08 18:03:10 +00:00
|
|
|
res.Body.Close() // per contract but unnecessary for most RoundTrippers
|
|
|
|
|
|
|
|
switch res.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return 0, false, nil
|
|
|
|
case http.StatusOK:
|
|
|
|
if size, err = strconv.ParseInt(res.Header["Content-Length"][0], 10, 64); err != nil {
|
2011-07-28 17:26:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return size, true, nil
|
2014-08-08 18:03:10 +00:00
|
|
|
default:
|
|
|
|
return 0, false, fmt.Errorf("Bad head response code: %v", res.Status)
|
2011-07-28 17:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
// PutObject uploads a Google Cloud Storage object.
|
2011-07-28 17:26:39 +00:00
|
|
|
// shouldRetry will be true if the put failed due to authorization, but
|
|
|
|
// credentials have been refreshed and another attempt is likely to succeed.
|
|
|
|
// In this case, content will have been consumed.
|
2014-08-16 05:27:15 +00:00
|
|
|
func (gsa *Client) PutObject(obj *Object, content io.Reader) (shouldRetry bool, err error) {
|
2014-08-08 18:03:10 +00:00
|
|
|
if err := obj.valid(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2014-08-16 05:27:15 +00:00
|
|
|
const maxSlurp = 2 << 20
|
|
|
|
var buf bytes.Buffer
|
|
|
|
n, err := io.CopyN(&buf, content, maxSlurp)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
contentType := http.DetectContentType(buf.Bytes())
|
|
|
|
if contentType == "application/octet-stream" && n < maxSlurp && utf8.Valid(buf.Bytes()) {
|
|
|
|
contentType = "text/plain; charset=utf-8"
|
|
|
|
}
|
|
|
|
|
2011-07-28 17:26:39 +00:00
|
|
|
objURL := gsAccessURL + "/" + obj.Bucket + "/" + obj.Key
|
|
|
|
var req *http.Request
|
2014-08-16 05:27:15 +00:00
|
|
|
if req, err = http.NewRequest("PUT", objURL, ioutil.NopCloser(io.MultiReader(&buf, content))); err != nil {
|
2011-07-28 17:26:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("x-goog-api-version", "2")
|
2014-08-16 05:27:15 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2011-07-28 17:26:39 +00:00
|
|
|
|
|
|
|
var resp *http.Response
|
|
|
|
if resp, err, shouldRetry = gsa.doRequest(req, false); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return shouldRetry, fmt.Errorf("Bad put response code: %v", resp.Status)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
// DeleteObject removes an object.
|
|
|
|
func (gsa *Client) DeleteObject(obj *Object) error {
|
|
|
|
if err := obj.valid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2011-07-28 17:26:39 +00:00
|
|
|
resp, err := gsa.simpleRequest("DELETE", gsAccessURL+"/"+obj.Bucket+"/"+obj.Key)
|
|
|
|
if err != nil {
|
2014-08-08 18:03:10 +00:00
|
|
|
return err
|
2011-07-28 17:26:39 +00:00
|
|
|
}
|
2014-08-08 18:03:10 +00:00
|
|
|
defer resp.Body.Close()
|
2011-07-28 17:26:39 +00:00
|
|
|
if resp.StatusCode != http.StatusNoContent {
|
2014-08-08 18:03:10 +00:00
|
|
|
return fmt.Errorf("Error deleting %v: bad delete response code: %v", obj, resp.Status)
|
2011-07-28 17:26:39 +00:00
|
|
|
}
|
2014-08-08 18:03:10 +00:00
|
|
|
return nil
|
2011-07-28 17:26:39 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
// EnumerateObjects lists the objects in a bucket.
|
|
|
|
// If after is non-empty, listing will begin with lexically greater object names.
|
|
|
|
// If limit is non-zero, the length of the list will be limited to that number.
|
2013-07-06 20:29:17 +00:00
|
|
|
func (gsa *Client) EnumerateObjects(bucket, after string, limit int) ([]SizedObject, error) {
|
2011-07-28 17:26:39 +00:00
|
|
|
// Build url, with query params
|
2014-08-08 18:03:10 +00:00
|
|
|
var params []string
|
2011-07-28 17:26:39 +00:00
|
|
|
if after != "" {
|
2011-08-25 15:14:47 +00:00
|
|
|
params = append(params, "marker="+url.QueryEscape(after))
|
2011-07-28 17:26:39 +00:00
|
|
|
}
|
|
|
|
if limit > 0 {
|
|
|
|
params = append(params, fmt.Sprintf("max-keys=%v", limit))
|
|
|
|
}
|
|
|
|
query := ""
|
|
|
|
if len(params) > 0 {
|
|
|
|
query = "?" + strings.Join(params, "&")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := gsa.simpleRequest("GET", gsAccessURL+"/"+bucket+"/"+query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("Bad enumerate response code: %v", resp.Status)
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
var xres struct {
|
|
|
|
Contents []SizedObject
|
|
|
|
}
|
2014-01-29 06:00:52 +00:00
|
|
|
defer httputil.CloseBody(resp.Body)
|
2014-08-08 18:03:10 +00:00
|
|
|
if err = xml.NewDecoder(resp.Body).Decode(&xres); err != nil {
|
2011-07-28 17:26:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-01-29 06:00:52 +00:00
|
|
|
|
2011-07-28 17:26:39 +00:00
|
|
|
// Fill in the Bucket on all the SizedObjects
|
2014-08-08 18:03:10 +00:00
|
|
|
for _, o := range xres.Contents {
|
|
|
|
o.Bucket = bucket
|
2011-07-28 17:26:39 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
return xres.Contents, nil
|
2011-07-28 17:26:39 +00:00
|
|
|
}
|
2014-08-03 04:00:43 +00:00
|
|
|
|
2014-08-08 18:03:10 +00:00
|
|
|
// BucketInfo returns information about a bucket.
|
2014-08-03 04:00:43 +00:00
|
|
|
func (c *Client) BucketInfo(bucket string) (*api.Bucket, error) {
|
|
|
|
return c.service.Buckets.Get(bucket).Do()
|
|
|
|
}
|