Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.4.0

Request #14751 Add S3 Object COPY operation
Submitted: 2008-10-06 03:27 UTC Modified: 2009-11-18 22:56 UTC
From: aashley Assigned: gauthierm
Status: Closed Package: Services_Amazon_S3 (version 0.2.0)
PHP Version: Irrelevant OS:
Roadmaps: 0.3.0    
Subscription  



Patch Object-copyFrom Revisions
Revision 2009-05-12 04:54 UTC
Developer aashley
 
Download patch

This patch renders other patches obsolete

Obsolete patches:

Index: Object.php
===================================================================
RCS file: /repository/pear/Services_Amazon_S3/S3/Resource/Object.php,v
retrieving revision 1.3
diff -u -r1.3 Object.php
--- Object.php	18 Dec 2008 22:59:43 -0000	1.3
+++ Object.php	12 May 2009 03:54:01 -0000
@@ -300,6 +300,55 @@
             $this->acl->save();
         }
     }
+
+    /**
+     * Copy data to this object from another S3 Object
+     *
+     * @param Services_Amazon_S3_Resource_Object
+     * @return void
+     */
+    public function copyFrom(Services_Amazon_S3_Resource_Object $source)
+    {
+        // When overwriting an object, the existing acl is lost
+        if ($this->exists && !$this->acl) {
+            $this->loadACL();
+        }
+
+        $headers = array();
+        $headers['x-amz-copy-source'] = '/' . rawurlencode($source->bucket->name) .
+                                        '/' . rawurlencode($source->key);
+                $headers['x-amz-metadata-directive'] = 'COPY';
+        foreach ($this->httpHeaders as $name => $value) {
+            $name = strtolower($name);
+            if (in_array($name, self::$allowedHeaders)) {
+                $headers[$name] = $value;
+            }
+        }
+        if (is_string($this->acl)) {
+            $headers['x-amz-acl'] = $this->acl;
+        }
+
+        $request = $this->s3->sendRequest($this, false, null,
+                                          HTTP_REQUEST_METHOD_PUT,
+                                          $headers);
+
+        $this->eTag = $request->getResponseHeader('etag');
+        if ($this->acl instanceof Services_Amazon_S3_AccessControlList) {
+            $this->acl->save();
+        }
+    }
+
+    /**
+     * Copy data from this object to another S3 Object
+     *
+     * @param Services_Amazon_S3_Resource_Object
+     * @return void
+     */
+    public function copyTo(Services_Amazon_S3_Resource_Object $target)
+    {
+        $target->copyFrom($this);
+    }
+
 }
 
 ?>