Skip to content

Commit

Permalink
Adds option to putObject() to check if the object was already there
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego de Estrada committed May 15, 2014
1 parent 7cce95a commit ba8f012
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,18 +610,27 @@ public static function inputResource(&$resource, $bufferSize = false, $md5sum =
* @param array $requestHeaders Array of request headers or content type as a string
* @param constant $storageClass Storage class constant
* @param constant $serverSideEncryption Server-side encryption
* @param boolean $checkExistence Whether to check if the object was already there
* @return boolean
*/
public static function putObject($input, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $requestHeaders = array(), $storageClass = self::STORAGE_CLASS_STANDARD, $serverSideEncryption = self::SSE_NONE)
public static function putObject($input, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $requestHeaders = array(), $storageClass = self::STORAGE_CLASS_STANDARD, $serverSideEncryption = self::SSE_NONE, $checkExistence = false)
{
if ($input === false) return false;
$rest = new S3Request('PUT', $bucket, $uri, self::$endpoint);

if (!is_array($input)) $input = array(
'data' => $input, 'size' => strlen($input),
'md5sum' => base64_encode(md5($input, true))
);

if ($checkExistence && isset($input['md5sum'])) {
$response = self::getObjectInfo($bucket, $uri);
if (isset($response['hash']) && bin2hex(base64_decode($input['md5sum'])) == $response['hash']) {
return true;
}
}

$rest = new S3Request('PUT', $bucket, $uri, self::$endpoint);

// Data
if (isset($input['fp']))
$rest->fp =& $input['fp'];
Expand Down

1 comment on commit ba8f012

@aymanrb
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an important pull request and must be merged ! It saved me a hell on syringing a 400K+ images directory based on DB records

Please sign in to comment.