Exemple #1
0
 protected function _copyKeys(array $keys, $targetPrefix, array $headers = array(), array $amzHeaders = array())
 {
     foreach ($keys as $key) {
         $currAmzHeaders = $amzHeaders;
         $this->_countKeys++;
         require_once 'Sitengine/Mime/Type.php';
         $mime = Sitengine_Mime_Type::get($key);
         $targetKey = $targetPrefix . $key;
         if ($this->_targetRenamePatternFind !== null && $this->_targetRenamePatternReplace !== null) {
             $targetKey = preg_replace($this->_targetRenamePatternFind, $this->_targetRenamePatternReplace, $targetKey);
         }
         try {
             $this->_print('copying: ' . $key . ' -> ' . $targetKey);
             $exclude = false;
             foreach ($this->_excludePatterns as $pattern) {
                 if (preg_match($pattern, $key)) {
                     $exclude = true;
                 }
             }
             if ($exclude) {
                 $this->_print(" -> EXCLUDED\n");
                 $this->_countExcluded++;
                 continue;
             }
             require_once 'Sitengine/Amazon/S3/Object.php';
             $targetObject = new Sitengine_Amazon_S3_Object($this->_connection, $this->_targetBucketName, $targetKey);
             $head = null;
             if (!$this->_force) {
                 # check if target exists
                 $head = $targetObject->head();
                 if ($head->getHttpResponse()->getStatus() == 200) {
                     $eTag = $head->getClient()->getLastResponse()->getHeader('Etag');
                     if ($eTag !== null) {
                         # set pre-condition
                         $currAmzHeaders[] = "x-amz-copy-source-if-none-match: {$eTag}";
                     }
                     /*
                     $this->_print(" -> TARGET EXISTS (SKIPPED)\n");
                     $this->_countSkipped++;
                     continue;
                     */
                 }
             }
             $response = $targetObject->copy($this->_sourceBucket->getName(), $key, $mime, $headers, $currAmzHeaders);
             $code = $response->getHttpResponse()->extractCode($response->getHttpResponse()->getHeadersAsString());
             if ($code == 412) {
                 $this->_print(" -> SKIPPED (EXISTS)\n");
                 $this->_countSkipped++;
                 continue;
             }
             if ($response->isError()) {
                 # try again
                 $response = $targetObject->copy($this->_sourceBucket->getName(), $key, $mime, $headers, $currAmzHeaders);
                 if ($response->isError()) {
                     #print $response->getErrorMessage()."\n";
                     require_once 'Sitengine/Amazon/S3/Utils/Exception.php';
                     throw new Sitengine_Amazon_S3_Utils_Exception('Copy Error');
                 }
             }
             if ($head !== null && $head->getHttpResponse()->getStatus() == 200) {
                 $this->_countReplaced++;
                 $this->_print(" -> REPLACE OK\n");
             } else {
                 $this->_countCopied++;
                 $this->_print(" -> COPY OK\n");
             }
             #$this->_countCopied++;
             #$this->_print(" -> OK\n");
             #$response = $targetObject->head($key);
             #print $response->getHttpResponse()->asString();
         } catch (Exception $exception) {
             $this->_countErrors++;
             $msg = " -> ERROR (" . $exception->getMessage() . ")\n";
             $this->_print($msg);
         }
     }
 }
 protected function _rollback()
 {
     #Sitengine_Debug::print_r($this->_newFiles);
     #Sitengine_Debug::print_r($this->_currentFiles);
     # remove new files
     foreach ($this->_newFiles as $fileId => $name) {
         #$key = $this->_configs[$fileId]['prefix'].'/'.$name;
         #$object = $this->_configs[$fileId]['object'];
         require_once 'Sitengine/Amazon/S3/Object.php';
         $object = new Sitengine_Amazon_S3_Object($this->_configs[$fileId]['connection'], $this->_configs[$fileId]['bucket'], $this->_configs[$fileId]['prefix'] . '/' . $name, $this->_configs[$fileId]['cname'], $this->_configs[$fileId]['ssl']);
         $response = $object->delete();
     }
     # restore current files
     foreach ($this->_currentFiles as $fileId => $name) {
         #$key = $this->_configs[$fileId]['prefix'].'/'.$name;
         $rollbackKey = 'Temp/' . $this->_configs[$fileId]['prefix'] . '/' . $name;
         #$object = $this->_configs[$fileId]['object'];
         require_once 'Sitengine/Amazon/S3/Object.php';
         $object = new Sitengine_Amazon_S3_Object($this->_configs[$fileId]['connection'], $this->_configs[$fileId]['bucket'], $this->_configs[$fileId]['prefix'] . '/' . $name, $this->_configs[$fileId]['cname'], $this->_configs[$fileId]['ssl']);
         require_once 'Sitengine/Amazon/S3/Object.php';
         $rollbackObject = new Sitengine_Amazon_S3_Object($this->_configs[$fileId]['connection'], $this->_configs[$fileId]['bucket'], $rollbackKey, $this->_configs[$fileId]['cname'], $this->_configs[$fileId]['ssl']);
         require_once 'Sitengine/Mime/Type.php';
         $mime = Sitengine_Mime_Type::get($name);
         $response = $object->copy($rollbackObject->getBucketName(), $rollbackKey, $mime, array(), $this->_configs[$fileId]['amzHeaders']);
         $response = $rollbackObject->delete();
     }
     $this->_newFiles = array();
     $this->_currentFiles = array();
 }