Example #1
1
 /**
  * @depends testPutObjectsWithUtf8Keys
  */
 public function testCopiesObjects()
 {
     self::log("Copying the object");
     $result = $this->client->copyObject(array('Bucket' => $this->bucket, 'Key' => 'copy-key', 'CopySource' => $this->bucket . '/' . self::TEST_KEY, 'MetadataDirective' => 'COPY', 'ServerSideEncryption' => 'AES256'));
     $this->assertNotEmpty($result['ETag']);
     $this->assertEquals('AES256', $result['ServerSideEncryption']);
     $this->assertNotEmpty($result['LastModified']);
     $this->client->waitUntil('object_exists', array('Bucket' => $this->bucket, 'Key' => 'copy-key'));
 }
Example #2
0
 /**
  * Creates a copy of an object that is already stored in Amazon S3.
  *
  * @param string $sourceBucket
  * @param string $sourceKey
  * @param string $targetBucket
  * @param string $targetKey
  * @param array  $params
  *
  * @return mixed
  */
 public function copyObject($sourceBucket, $sourceKey, $targetBucket, $targetKey, array $params = [])
 {
     $params['Bucket'] = $targetBucket;
     $params['Key'] = $targetKey;
     $params['CopySource'] = urlencode($sourceBucket . '/' . $sourceKey);
     return $this->instance->copyObject($params);
 }
 /**
  * rename file/directory
  *
  * @return string
  */
 public function rename()
 {
     $oldPath = $this->sanitizePath($this->get['old']);
     $isDir = $this->isDir($this->metadata("{$oldPath}"));
     $oldPath .= $isDir ? '/' : '';
     $contents = $isDir ? $this->s3->listObjects(['Bucket' => $this->bucket, 'Prefix' => $oldPath])['Contents'] : array();
     // NOTE: we do this instead of isDir as we only care if there are files under this prefix
     if (count($contents) > 1) {
         $this->error('Unfortunately, we are currently unable to rename a non-empty directory.', false);
     }
     $pathInfo = pathinfo($oldPath);
     $dirName = $pathInfo['dirname'];
     $baseName = $pathInfo['basename'];
     $newFile = $this->get['new'];
     $newPath = join('/', array($dirName, $newFile));
     if ($isDir) {
         $response = $this->createDirectory($newPath);
     } else {
         $response = $this->s3->copyObject(['Bucket' => $this->bucket, 'CopySource' => urlencode($this->bucket . '/' . $oldPath), 'Key' => $newPath]);
         //				array('bucket' => $this->bucket, 'filename' => $oldPath),
         //				array('bucket' => $this->bucket, 'filename' => $newPath),
         //				array('acl' => CannedAcl::PUBLIC_READ)
         //			);
     }
     if (!empty($response)) {
         $this->s3->deleteObject(['Bucket' => $this->bucket, 'Key' => $oldPath]);
         //$this->bucket, $oldPath);
     }
     return array('Error' => '', 'Code' => 0, 'Old Path' => $oldPath, 'Old Name' => $baseName, 'New Path' => $newPath, 'New Name' => $newFile);
 }
Example #4
0
 /**
  * Copy Object
  *
  * @param string $from Path From
  * @param string $to   Path To
  *
  * @return bool
  */
 public function copy($from, $to)
 {
     $from = ltrim($from, "/");
     $to = ltrim($to, "/");
     if (!$this->exists($from)) {
         $path = $this->bucket . "/" . $from;
         throw new ExceptionStorage("Object {$path} Not found", static::E_NOT_IS_DIR);
     }
     if ($this->exists($to)) {
         $path = $this->bucket . "/" . $to;
         throw new ExceptionStorage("Object {$path} exists");
     }
     // Copia Object
     $this->clientS3->copyObject(array('Bucket' => $this->bucket, 'Key' => $to, 'CopySource' => $this->bucket . '/' . rawurlencode($from), 'ACL' => static::ACL_PUBLIC_READ, 'MetadataDirective' => 'COPY'));
     $pathFrom = trim($from, '/') . '/';
     $iterator = $this->clientS3->getIterator('ListObjects', array('Bucket' => $this->bucket, 'Prefix' => $pathFrom));
     foreach ($iterator as $object) {
         $pattern = preg_quote($pathFrom, "/");
         $Key = preg_replace("/^{$pattern}/", "", $object['Key']);
         if (!empty($Key)) {
             $pattern = preg_quote($pathFrom, "/");
             $Key = preg_replace("/^{$pattern}/", "", $object['Key']);
             $origem = $this->bucket . '/' . $object['Key'];
             $destino = trim($to, "/") . "/" . $Key;
             // Copia Object
             $this->clientS3->copyObject(array('Bucket' => $this->bucket, 'Key' => $destino, 'CopySource' => $origem, 'ACL' => static::ACL_PUBLIC_READ, 'MetadataDirective' => 'COPY'));
         }
     }
     return true;
 }
Example #5
0
 /**
  * Rename a file
  *
  * @param   string  $path
  * @param   string  $newpath
  * @return  array   file metadata
  */
 public function rename($path, $newpath)
 {
     $options = $this->getOptions($newpath, array('Bucket' => $this->bucket, 'CopySource' => $this->bucket . '/' . $this->prefix($path)));
     $result = $this->client->copyObject($options)->getAll();
     $result = $this->normalizeObject($result, $newpath);
     $this->delete($path);
     return $result;
 }
 /**
  *
  * @param string $oldName
  * @param string $newName
  *
  * @return bool - true
  */
 public function rename($oldName, $newName)
 {
     try {
         $this->s3->copyObject(['Bucket' => $this->bucket, 'Key' => $newName, 'CopySource' => sprintf('%s/%s', $this->bucket, $oldName)]);
         $this->delete($oldName);
     } catch (\Exception $e) {
         throw new FileException('File was not renamed', $e->getCode(), $e);
     }
     return true;
 }
Example #7
0
 /**
  * @param string $container
  * @param string $name
  * @param string $src_container
  * @param string $src_name
  * @param array  $properties
  *
  * @throws DfException
  *
  * @param array  $properties
  */
 public function copyBlob($container = '', $name = '', $src_container = '', $src_name = '', $properties = [])
 {
     try {
         $this->checkConnection();
         $options = ['Bucket' => $container, 'Key' => $name, 'CopySource' => urlencode($src_container . '/' . $src_name)];
         $result = $this->blobConn->copyObject($options);
     } catch (\Exception $ex) {
         throw new DfException('Failed to copy blob "' . $name . '": ' . $ex->getMessage());
     }
 }
 /**
  * Renamed DFS file $oldPath to DFS file $newPath
  *
  * @param string $oldPath
  * @param string $newPath
  *
  * @return bool
  */
 public function renameOnDFS($oldPath, $newPath)
 {
     try {
         $this->s3client->copyObject(array('Bucket' => $this->bucket, 'Key' => $newPath, 'CopySource' => $this->bucket . "/" . $oldPath, 'ACL' => 'public-read'));
         $this->delete($oldPath);
         return true;
     } catch (S3Exception $e) {
         eZDebug::writeError($e->getMessage(), __METHOD__);
         return false;
     }
 }
 /**
  * Copy file into another file
  * S3 does not preserve ACL for a file
  * also can be used only for file smaller than 5GB
  *
  * @param  string  $source     source file path
  * @param  string  $targetDir  target directory path
  * @param  string  $name       new file name
  * @return Guzzle\Service\Resource\Model
  **/
 protected function _copy($source, $targetDir, $name)
 {
     $stat = $this->stat($source);
     if ($stat['mime'] == 'directory') {
         $newDir = $this->_mkdir($targetDir, $name);
         $items = $this->_scandir($source);
         foreach ($items as $item) {
             $this->_copy($item, $newDir, basename($item));
         }
         $this->clearcache();
         return $newDir;
     }
     $this->clearcache();
     return $this->s3->copyObject(array("Bucket" => $this->bucket, "Key" => $this->pathToKey($this->_joinPath($targetDir, $name)), "CopySource" => $this->bucket . "/" . $this->pathToKey($source), "ACL" => $this->options['acl']));
 }
Example #10
0
 /**
  * Copy a file internally.
  *
  * @param  string            $src
  * @param  string            $dest
  * @return boolean           Whether the file was copied
  * @throws \RuntimeException
  */
 public function copy($src, $dest)
 {
     if (null !== $this->filenameFilter) {
         $src = $this->filenameFilter->filter($src);
         $dest = $this->filenameFilter->filter($dest);
     }
     $params = array('Bucket' => $this->getBucket(), 'Key' => $dest, 'ACL' => $this->getAcl(), 'StorageClass' => $this->getStorageClass(), 'CopySource' => '/' . $this->getBucket() . '/' . rawurlencode($src));
     try {
         $this->s3Client->copyObject($params);
     } catch (S3Exception $e) {
         if (!$this->getThrowExceptions()) {
             return false;
         }
         throw new \RuntimeException('Exception thrown by Aws\\S3\\S3Client: ' . $e->getMessage(), null, $e);
     }
     return true;
 }
Example #11
0
 public function copy($path1, $path2)
 {
     $path1 = $this->normalizePath($path1);
     $path2 = $this->normalizePath($path2);
     if ($this->is_file($path1)) {
         try {
             $result = $this->connection->copyObject(array('Bucket' => $this->bucket, 'Key' => $this->cleanKey($path2), 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1)));
             $this->testTimeout();
         } catch (S3Exception $e) {
             \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
             return false;
         }
     } else {
         if ($this->file_exists($path2)) {
             return false;
         }
         try {
             $result = $this->connection->copyObject(array('Bucket' => $this->bucket, 'Key' => $path2 . '/', 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1 . '/')));
             $this->testTimeout();
         } catch (S3Exception $e) {
             \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
             return false;
         }
         $dh = $this->opendir($path1);
         if (is_resource($dh)) {
             while (($file = readdir($dh)) !== false) {
                 if ($file === '.' || $file === '..') {
                     continue;
                 }
                 $source = $path1 . '/' . $file;
                 $target = $path2 . '/' . $file;
                 $this->copy($source, $target);
             }
         }
     }
     return true;
 }
Example #12
0
 /**
  * Publishes the given persistent resource from the given storage
  *
  * @param \TYPO3\Flow\Resource\Resource $resource The resource to publish
  * @param CollectionInterface $collection The collection the given resource belongs to
  * @return void
  * @throws Exception
  */
 public function publishResource(Resource $resource, CollectionInterface $collection)
 {
     $storage = $collection->getStorage();
     if ($storage instanceof S3Storage) {
         if ($storage->getBucketName() === $this->bucketName && $storage->getKeyPrefix() === $this->keyPrefix) {
             throw new Exception(sprintf('Could not publish resource with SHA1 hash %s of collection %s because the source and target S3 bucket is the same, with identical key prefixes. Either choose a different bucket or at least key prefix for the target.', $resource->getSha1(), $collection->getName()), 1428929563);
         }
         try {
             $sourceObjectArn = $storage->getBucketName() . '/' . $storage->getKeyPrefix() . $resource->getSha1();
             $objectName = $this->keyPrefix . $this->getRelativePublicationPathAndFilename($resource);
             $options = array('ACL' => 'public-read', 'Bucket' => $this->bucketName, 'CopySource' => urlencode($sourceObjectArn), 'ContentType' => $resource->getMediaType(), 'MetadataDirective' => 'REPLACE', 'Key' => $objectName);
             $this->s3Client->copyObject($options);
             $this->systemLogger->log(sprintf('Successfully published resource as object "%s" (MD5: %s) by copying from bucket "%s" to bucket "%s"', $objectName, $resource->getMd5() ?: 'unknown', $storage->getBucketName(), $this->bucketName), LOG_DEBUG);
         } catch (S3Exception $e) {
             throw new Exception(sprintf('Could not publish resource with SHA1 hash %s of collection %s (source object: %s) through "CopyObject" because the S3 client reported an error: %s', $resource->getSha1(), $collection->getName(), $sourceObjectArn, $e->getMessage()), 1428999574);
         }
     } else {
         $sourceStream = $resource->getStream();
         if ($sourceStream === false) {
             throw new Exception(sprintf('Could not publish resource with SHA1 hash %s of collection %s because there seems to be no corresponding data in the storage.', $resource->getSha1(), $collection->getName()), 1428929649);
         }
         $this->publishFile($sourceStream, $this->getRelativePublicationPathAndFilename($resource), $resource);
     }
 }
Example #13
0
 /**
  * copyObject
  *
  * @param array $params
  *
  * @return \Guzzle\Service\Resource\Model
  */
 public function copyObject(array $params = array())
 {
     $params['Bucket'] = $this->name;
     return $this->client->copyObject($params);
 }
Example #14
0
 /**
  * Copy a file
  *
  * @param   string  $path
  * @param   string  $newpath
  * @return  array   file metadata
  */
 public function copy($path, $newpath)
 {
     $options = $this->getOptions($newpath, array('Bucket' => $this->bucket, 'CopySource' => $this->bucket . '/' . $this->applyPathPrefix($path), 'ACL' => $this->getObjectACL($path)));
     $result = $this->client->copyObject($options)->getAll();
     return $this->normalizeObject($result, $newpath);
 }