From cca73ca50e7792f417b2fad4d01bd4f2304d63f2 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Tue, 7 Oct 2014 06:47:58 -0700 Subject: [PATCH 1/2] More samples around setting content types. --- .../update-object-content-type.php | 57 +++++++++++++++++ .../upload-object-with-content-type.php | 61 +++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 samples/ObjectStore/update-object-content-type.php create mode 100644 samples/ObjectStore/upload-object-with-content-type.php diff --git a/samples/ObjectStore/update-object-content-type.php b/samples/ObjectStore/update-object-content-type.php new file mode 100644 index 000000000..bda93a8b3 --- /dev/null +++ b/samples/ObjectStore/update-object-content-type.php @@ -0,0 +1,57 @@ + getenv('RAX_USERNAME'), + 'apiKey' => getenv('RAX_API_KEY') +)); + +// 2. Obtain an Object Store service object from the client. +$region = 'DFW'; +$objectStoreService = $client->objectStoreService(null, $region); + +// 3. Get container. +$container = $objectStoreService->getContainer('logos'); + +// 4. Get object. +$objectName = 'php-elephant.jpg'; +$object = $container->getObject($objectName); + +// 5. Update object metadata. +$object->setContentType('image/jpeg'); +$object->update(); + +// IMPORTANT NOTE: Even though you are only updating the content type of the +// uploaded object, you are downloading the entire object first (via +// $container->getObject($objectName) and re-uploading it (via $object->update). +// There is no other way to update the object's content type once it has +// be uploaded. diff --git a/samples/ObjectStore/upload-object-with-content-type.php b/samples/ObjectStore/upload-object-with-content-type.php new file mode 100644 index 000000000..528b67bd3 --- /dev/null +++ b/samples/ObjectStore/upload-object-with-content-type.php @@ -0,0 +1,61 @@ + getenv('RAX_USERNAME'), + 'apiKey' => getenv('RAX_API_KEY') +)); + +// 2. Obtain an Object Store service object from the client. +$region = 'DFW'; +$objectStoreService = $client->objectStoreService(null, $region); + +// 3. Get container. +$container = $objectStoreService->getContainer('logos'); + +// 4. Setup headers with custom content type. +// NOTE: Normally you do NOT need to specify the content type. The +// service will automatically guess it for you. If, however, you +// are using an obscure type of content or want to be explicit about +// the content type rather than letting the service guess at it, +// you may specify it as shown below. +$customHeaders = array( + 'Content-Type' => 'image/gif' +); + +// 5. Upload an object to the container. +$localFileName = __DIR__ . '/php-elephant.jpg'; +$remoteFileName = 'php-elephant.jpg'; + +$fileData = fopen($localFileName, 'r'); +$container->uploadObject($remoteFileName, $fileData, $customHeaders); + +// Note that while we call fopen to open the file resource, we do not call fclose at the end. +// The file resource is automatically closed inside the uploadObject call. From 50922905cb097d67c46efa582a6169df406a0d36 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Tue, 7 Oct 2014 07:26:09 -0700 Subject: [PATCH 2/2] Using more efficient method to update content type (thanks @jamiehannaford!) --- samples/ObjectStore/update-object-content-type.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/samples/ObjectStore/update-object-content-type.php b/samples/ObjectStore/update-object-content-type.php index bda93a8b3..cd6613172 100644 --- a/samples/ObjectStore/update-object-content-type.php +++ b/samples/ObjectStore/update-object-content-type.php @@ -44,14 +44,7 @@ // 4. Get object. $objectName = 'php-elephant.jpg'; -$object = $container->getObject($objectName); +$object = $container->getPartialObject($objectName); -// 5. Update object metadata. -$object->setContentType('image/jpeg'); -$object->update(); - -// IMPORTANT NOTE: Even though you are only updating the content type of the -// uploaded object, you are downloading the entire object first (via -// $container->getObject($objectName) and re-uploading it (via $object->update). -// There is no other way to update the object's content type once it has -// be uploaded. +// 5. Update object content type. +$object->saveMetadata(array('Content-Type' => 'image/jpeg'), false);