From ba8f01229b8dfbb57a940079def7604745b75012 Mon Sep 17 00:00:00 2001 From: Diego de Estrada Date: Wed, 14 May 2014 23:14:20 -0300 Subject: [PATCH] Adds option to putObject() to check if the object was already there --- S3.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/S3.php b/S3.php index 2078f929..b616fbfb 100644 --- a/S3.php +++ b/S3.php @@ -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'];