Skip to content
This repository was archived by the owner on Dec 19, 2017. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,11 @@ func (b *Bucket) PutReaderHeader(path string, r io.Reader, length int64, customH
}

/*
Copy - copy objects inside bucket
copyObject - copy objects.
oldPath should begin with /<bucket-name>.
newPath should not include the bucket name.
*/
func (b *Bucket) Copy(oldPath, newPath string, perm ACL) error {
func (b *Bucket) copyObject(oldPath, newPath string, perm ACL) error {
if !strings.HasPrefix(oldPath, "/") {
oldPath = "/" + oldPath
}
Expand All @@ -343,7 +345,7 @@ func (b *Bucket) Copy(oldPath, newPath string, perm ACL) error {
bucket: b.Name,
path: newPath,
headers: map[string][]string{
"x-amz-copy-source": {amazonEscape("/" + b.Name + oldPath)},
"x-amz-copy-source": {amazonEscape(oldPath)},
"x-amz-acl": {string(perm)},
},
}
Expand All @@ -366,6 +368,26 @@ func (b *Bucket) Copy(oldPath, newPath string, perm ACL) error {
panic("unreachable")
}

/*
Copy - copy objects inside bucket
*/
func (b *Bucket) Copy(oldPath, newPath string, perm ACL) error {
if !strings.HasPrefix(oldPath, "/") {
oldPath = "/" + oldPath
}
return b.copyObject("/" + b.Name + oldPath, newPath, perm)
}

/*
CopyToAnotherBucket - copy objects from this bucket to another
*/
func (b *Bucket) CopyToAnotherBucket(oldPath string, newBucket *Bucket, newPath string, perm ACL) error {
if !strings.HasPrefix(oldPath, "/") {
oldPath = "/" + oldPath
}
return newBucket.copyObject("/" + b.Name + oldPath, newPath, perm)
}

// Del removes an object from the S3 bucket.
//
// See http://goo.gl/APeTt for details.
Expand Down
20 changes: 20 additions & 0 deletions s3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,26 @@ func (s *S) TestCopy(c *C) {
c.Assert(req.Header["X-Amz-Acl"], DeepEquals, []string{"private"})
}

func (s *S) TestCopyToAnotherBucket(c *C) {
testServer.Response(200, nil, "")

b := s.s3.Bucket("bucket")
nb := s.s3.Bucket("new-bucket")
err := b.CopyToAnotherBucket(
"old/file",
nb,
"new/file",
s3.Private,
)
c.Assert(err, IsNil)

req := testServer.WaitRequest()
c.Assert(req.Method, Equals, "PUT")
c.Assert(req.URL.Path, Equals, "/new-bucket/new/file")
c.Assert(req.Header["X-Amz-Copy-Source"], DeepEquals, []string{"/bucket/old/file"})
c.Assert(req.Header["X-Amz-Acl"], DeepEquals, []string{"private"})
}

func (s *S) TestPlusInURL(c *C) {
testServer.Response(200, nil, "")

Expand Down