/** * @depends testHeadBucket */ public function notestPutAndListObjects() { $this->client->waitUntil('bucket_exists', array('Bucket' => $this->bucket)); $command = $this->client->getCommand('PutObject', array('Bucket' => $this->bucket, 'Key' => self::TEST_KEY, 'ContentMD5' => true, 'Body' => 'åbc 123', 'ContentType' => 'application/foo', 'ACP' => $this->acp, 'Metadata' => array('test' => '123', 'abc' => '@pples', 'foo' => '', 'null' => null, 'space' => ' ', 'zero' => '0', 'trim' => ' hi '))); self::log("Uploading an object"); $result = $command->execute(); // make sure the expect header wasn't sent $this->assertNull($command->getRequest()->getHeader('Expect')); $this->assertInstanceOf('Guzzle\\Service\\Resource\\Model', $result); $this->assertNotEmpty($result['ETag']); $this->client->waitUntil('object_exists', array('Bucket' => $this->bucket, 'Key' => self::TEST_KEY)); self::log("HEAD the object"); $result = $this->client->headObject(array('Bucket' => $this->bucket, 'Key' => self::TEST_KEY)); $this->assertEquals('application/foo', $result['ContentType']); $this->assertEquals('123', $result['Metadata']['test']); $this->assertEquals('@pples', $result['Metadata']['abc']); // Ensure the object was created correctly self::log("GETting the object"); $result = $this->client->getObject(array('Bucket' => $this->bucket, 'Key' => self::TEST_KEY)); $this->assertInstanceOf('Guzzle\\Service\\Resource\\Model', $result); $this->assertInstanceOf('Guzzle\\Http\\EntityBody', $result['Body']); $this->assertEquals('åbc 123', (string) $result['Body']); $this->assertEquals('application/foo', $result['ContentType']); $this->assertEquals('123', $result['Metadata']['test']); $this->assertEquals('@pples', $result['Metadata']['abc']); // Ensure the object was created and we can find it in the iterator self::log("Checking if the item is in the ListObjects results"); $iterator = $this->client->getIterator('ListObjects', array('Bucket' => $this->bucket, 'Prefix' => self::TEST_KEY)); $objects = $iterator->toArray(); $this->assertEquals(1, count($objects)); $this->assertEquals('foo', $objects[0]['Key']); }
/** * Returns size of a file in the DFS backend, from a relative path. * * @param string $filePath The relative file path we want to get size of * * @return int */ public function getDfsFileSize($filePath) { try { $object = $this->s3client->headObject(array('Bucket' => $this->bucket, 'Key' => $filePath)); return $object['ContentLength']; } catch (S3Exception $e) { eZDebug::writeError($e->getMessage(), __METHOD__); return false; } }
protected function getMetaData($path) { if (!array_key_exists($path, $this->mdCache)) { try { $this->mdCache[$path] = $this->s3->headObject(['Bucket' => $this->bucket, 'Key' => $path])->toArray(); } catch (\Exception $e) { throw new FileException('Unable to get metadata for file: ' . $path, $e->getCode(), $e); } } return $this->mdCache[$path]; }
/** * List blob * * @param string $container Container name * @param string $name Blob name * * @return array instance * @throws DfException */ public function getBlobProperties($container, $name) { try { $this->checkConnection(); /** @var \Aws\Result $result */ $result = $this->blobConn->headObject(['Bucket' => $container, 'Key' => $name]); $out = ['name' => $name, 'content_type' => $result->get('ContentType'), 'content_length' => intval($result->get('ContentLength')), 'last_modified' => $result->get('LastModified')]; return $out; } catch (\Exception $ex) { throw new DfException('Failed to list blob metadata: ' . $ex->getMessage()); } }
/** * Return stat for given path. * Stat contains following fields: * - (int) size file size in b. required * - (int) ts file modification time in unix time. required * - (string) mime mimetype. required for folders, others - optionally * - (bool) read read permissions. required * - (bool) write write permissions. required * - (bool) locked is object locked. optionally * - (bool) hidden is object hidden. optionally * - (string) alias for symlinks - link target path relative to root path. optionally * - (string) target for symlinks - link target path. optionally * * If file does not exists - returns empty array or false. * * @param string $path file path * @return array|false * @author Dmitry (dio) Levashov, * @author Alexey Sukhotin **/ protected function _stat($path) { $stat = array('size' => 0, 'ts' => time(), 'read' => true, 'write' => true, 'locked' => false, 'hidden' => false, 'mime' => 'directory'); // S3 apparently doesn't understand paths Key with a "/" at the end if (substr($path, -1) == "/") { $path = substr($path, 0, strlen($path) - 1); } if ($this->root == $path) { return $stat; } $np = $this->_normpath($path); /* @var $obj \Guzzle\Service\Resource\Model */ try { $obj = $this->s3->headObject(['Bucket' => $this->options['bucket'], 'Key' => $np]); } catch (NoSuchKeyException $e) { } if (!isset($obj)) { $np .= '/'; try { $obj = $this->s3->headObject(['Bucket' => $this->options['bucket'], 'Key' => $np]); } catch (NoSuchKeyException $e) { } } // No obj means it's a folder, or it really doesn't exist if (!isset($obj)) { if (!$this->_scandir($path)) { return false; } else { return $stat; } } $mime = ''; if ($obj->hasKey('Last-Modified')) { $stat['ts'] = strtotime($obj->get('Last-Modified')); } try { $files = $this->s3->listObjects(['Bucket' => $this->options['bucket'], 'Prefix' => $np, 'Delimiter' => '/'])->get('Contents'); } catch (Exception $e) { } $mime = $obj->get('ContentType'); $stat['mime'] = substr($np, -1) == '/' ? 'directory' : (!$mime ? 'text/plain' : $mime); foreach ($files as $file) { if ($file['Key'] == $np) { $stat['size'] = $file['Size']; } } return $stat; }
public function getMimeType($path) { $path = $this->normalizePath($path); if ($this->is_dir($path)) { return 'httpd/unix-directory'; } else { if ($this->file_exists($path)) { try { $result = $this->connection->headObject(array('Bucket' => $this->bucket, 'Key' => $this->cleanKey($path))); } catch (S3Exception $e) { \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); return false; } return $result['ContentType']; } } return false; }
/** * Try to determine and return a files MIME content type. * * @param string $file * @return string The MIME content type * @throws \RuntimeException */ public function type($file) { if (null !== $this->filenameFilter) { $file = $this->filenameFilter->filter($file); } $params = array('Bucket' => $this->getBucket(), 'Key' => $file); try { $response = $this->s3Client->headObject($params); } catch (S3Exception $e) { if (!$this->getThrowExceptions()) { return null; } throw new \RuntimeException('Exception thrown by Aws\\S3\\S3Client: ' . $e->getMessage(), null, $e); } $contentType = (string) $response->get('ContentType'); if ('' === $contentType) { return null; } return $contentType; }
/** * Get metadata for a file * * @param string $path * @return array file metadata */ public function getMetadata($path) { $options = $this->getOptions($path); $result = $this->client->headObject($options); return $this->normalizeObject($result->getAll(), $path); }
/** * headObject * * @param string $key * @param array $params * * @return \Guzzle\Service\Resource\Model */ public function headObject($key, array $params = array()) { $params['Key'] = $key; $params['Bucket'] = $this->name; return $this->client->headObject($params); }
/** * Get object expiration time. * @param string $cacheKey get the cache key. * @return integer|boolean the expiration time, false if not found. */ private function getObjectExpirationTime($cacheKey) { try { $result = $this->_client->headObject(['Bucket' => $this->bucket, 'Key' => $cacheKey]); return strtotime($result['Expires']); } catch (\Aws\S3\Exception\NoSuchKeyException $exc) { return false; } }
protected function fetchObject(S3Client $client, Bucket $bucket, $key, $store = true, $maxSize = null) { try { // Make sure the file exists. $info = $client->headObject(array('Bucket' => $bucket->getName(), 'Key' => $key)); $contentLength = $info->get('ContentLength'); if ($contentLength == 0) { // This key is likely a directory. return false; } if ($maxSize !== NULL && $maxSize < $contentLength) { // The file is too large. return false; } } catch (\Exception $e) { return false; } $result = $client->getObject(array('Bucket' => $bucket->getName(), 'Key' => $key)); $data = $result->get('Body'); if ($store) { $this->writeToDrive($bucket->getSaveMethod(), $data, $bucket->getDestination(), $key); unset($result); unset($data); return true; } else { unset($result); return $data; } }
/** * retrieve a file's metadata * * @param string $filePath * @return \Guzzle\Service\Resource\Model */ protected function metadata($filePath) { return $this->s3->headObject(['Bucket' => $this->bucket, 'Key' => $filePath]); // return $this->s3->get_object_metadata($this->bucket, $filePath); }