Skip to content

Commit ba8f012

Browse files
author
Diego de Estrada
committed
Adds option to putObject() to check if the object was already there
1 parent 7cce95a commit ba8f012

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

S3.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,18 +610,27 @@ public static function inputResource(&$resource, $bufferSize = false, $md5sum =
610610
* @param array $requestHeaders Array of request headers or content type as a string
611611
* @param constant $storageClass Storage class constant
612612
* @param constant $serverSideEncryption Server-side encryption
613+
* @param boolean $checkExistence Whether to check if the object was already there
613614
* @return boolean
614615
*/
615-
public static function putObject($input, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $requestHeaders = array(), $storageClass = self::STORAGE_CLASS_STANDARD, $serverSideEncryption = self::SSE_NONE)
616+
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)
616617
{
617618
if ($input === false) return false;
618-
$rest = new S3Request('PUT', $bucket, $uri, self::$endpoint);
619619

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

625+
if ($checkExistence && isset($input['md5sum'])) {
626+
$response = self::getObjectInfo($bucket, $uri);
627+
if (isset($response['hash']) && bin2hex(base64_decode($input['md5sum'])) == $response['hash']) {
628+
return true;
629+
}
630+
}
631+
632+
$rest = new S3Request('PUT', $bucket, $uri, self::$endpoint);
633+
625634
// Data
626635
if (isset($input['fp']))
627636
$rest->fp =& $input['fp'];

0 commit comments

Comments
 (0)