本文整理汇总了PHP中Aws\S3\S3Client::getObject方法的典型用法代码示例。如果您正苦于以下问题:PHP S3Client::getObject方法的具体用法?PHP S3Client::getObject怎么用?PHP S3Client::getObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Aws\S3\S3Client
的用法示例。
在下文中一共展示了S3Client::getObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get
/**
* {@inheritdoc}
*/
public function get($path)
{
try {
$model = $this->s3->getObject(['Bucket' => $this->bucket, 'Key' => $path]);
return (string) $model->get('Body');
} catch (S3Exception $e) {
if ($e->getAwsErrorCode() == 'NoSuchKey') {
throw Exception\NotFoundException::pathNotFound($path, $e);
}
throw Exception\StorageException::getError($path, $e);
}
}
示例2: download
/**
* S3の指定パスのファイルをダウンロードする
*
* @param string $path
* @return string
**/
public function download($path)
{
try {
$result = $this->client->getObject(array('Bucket' => $this->bucket, 'Key' => $path));
$response = '';
$result['Body']->rewind();
while ($data = $result['Body']->read(1024)) {
$response .= $data;
}
} catch (\Exception $e) {
throw $e;
}
return $response;
}
示例3: getFileIfNewest
/**
* @param string $localFile
* @param string $remoteFile
* @param int $perm
* @return string
*/
function getFileIfNewest($localFile, $remoteFile, $perm = 0777)
{
$this->lastRemoteFile = $remoteFile;
$download = false;
if (!file_exists($localFile)) {
$download = true;
} else {
$iterator = $this->s3Client->getIterator('ListObjects', array('Bucket' => $this->bucket, 'Prefix' => $remoteFile, 'Delimiter' => '/'));
foreach ($iterator as $object) {
$remoteDate = date("U", strtotime($object['LastModified']));
$localDate = filemtime($localFile);
if ($remoteDate > $localDate) {
$download = true;
}
break;
}
}
if ($download) {
try {
$result = $this->s3Client->getObject(array('Bucket' => $this->bucket, 'Key' => $remoteFile));
} catch (\Exception $e) {
error_log("Error recovering {$remoteFile} from S3: " . $e->getMessage());
return null;
}
file_put_contents($localFile, $result['Body']);
chmod($localFile, $perm);
touch($localFile, strtotime($result['LastModified']));
}
return $localFile;
}
示例4: fetch
/**
* {@inheritDoc}
*
* @link http://stackoverflow.com/questions/13686316/grabbing-contents-of-object-from-s3-via-php-sdk-2
*/
public function fetch($path)
{
$result = $this->s3->getObject(array('Bucket' => $this->getConfigRelativeKey('s3Bucket'), 'Key' => $path));
$body = $result->get('Body');
$body->rewind();
$content = $body->read($result['ContentLength']);
return $content;
}
示例5: get
/**
* {@inheritdoc}
*/
public function get($path)
{
try {
$model = $this->s3->getObject(['Bucket' => $this->bucket, 'Key' => $path]);
return (string) $model->get('Body');
} catch (NoSuchKeyException $e) {
throw Exception\NotFoundException::pathNotFound($path, $e);
} catch (AwsExceptionInterface $e) {
throw Exception\StorageException::getError($path, $e);
}
}
示例6: testPutAndListObjects
/**
* @depends testHeadBucket
*/
public function testPutAndListObjects()
{
$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')));
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', $this->bucket . '/' . 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']);
}
示例7: tempOriginal
/**
* Tells the driver to prepare a copy of the original file locally.
*
* @param FileModel $fileModel
* @return File
*/
public function tempOriginal(FileModel $fileModel)
{
// Recreate original filename
$tempOriginalPath = tempnam(sys_get_temp_dir(), null);
$originalPath = $this->nameGenerator->fileName($fileModel);
// Download file
$this->s3->getObject(array('Bucket' => $this->awsBucket, 'Key' => $originalPath, 'SaveAs' => $tempOriginalPath));
return new File($tempOriginalPath);
}
示例8: tempOriginal
/**
* Tells the driver to prepare a copy of the original image locally.
*
* @param Image $image
* @return File
*/
public function tempOriginal(Image $image)
{
// Recreate original filename
$tempOriginalPath = tempnam(sys_get_temp_dir(), null);
$originalPath = sprintf('%s-%s.%s', $image->getKey(), $this->generateHash($image), Mime::getExtensionForMimeType($image->mime_type));
// Download file
$this->s3->getObject(array('Bucket' => $this->awsBucket, 'Key' => $originalPath, 'SaveAs' => $tempOriginalPath));
return new File($tempOriginalPath);
}
示例9: getContents
/**
* Returns the binary content of $filePath from DFS
*
* @param string $filePath local file path
*
* @return binary|bool file's content, or false
*/
public function getContents($filePath)
{
try {
$object = $this->s3client->getObject(array('Bucket' => $this->bucket, 'Key' => $filePath));
return (string) $object['Body'];
} catch (S3Exception $e) {
eZDebug::writeError($e->getMessage(), __METHOD__);
return false;
}
}
示例10: testWorksWithPrefixKeys
/**
* @depends testPutAndListObjects
* @dataProvider prefixKeyProvider
*/
public function testWorksWithPrefixKeys($key, $cleaned, $encoded)
{
$this->client->waitUntil('bucket_exists', array('Bucket' => $this->bucket));
$command = $this->client->getCommand('PutObject', array('Bucket' => $this->bucket, 'Key' => $key, 'SourceFile' => __FILE__));
$command->execute();
// Ensure the path is correct
$this->assertEquals($encoded, $command->getRequest()->getPath());
// Ensure the key is not an array and is returned to it's previous value
$this->assertEquals($key, $command['Key']);
$this->client->waitUntil('object_exists', array('Bucket' => $this->bucket, 'Key' => $key));
$result = $this->client->getObject(array('Bucket' => $this->bucket, 'Key' => $key));
$this->assertEquals(file_get_contents(__FILE__), (string) $result['Body']);
// Test using path style hosting
$command = $this->client->getCommand('DeleteObject', array('Bucket' => $this->bucket, 'Key' => $key, 'PathStyle' => true));
$command->execute();
$this->assertEquals('/' . $this->bucket . $encoded, $command->getRequest()->getPath());
}
示例11: stream
/**
* Return a read-only stream resource for a file.
*
* @param string $file
* @return resource|boolean The resource or false on failure
* @throws \RuntimeException
*/
public function stream($file)
{
if (null !== $this->filenameFilter) {
$file = $this->filenameFilter->filter($file);
}
$params = array('Bucket' => $this->getBucket(), 'Key' => $file);
try {
$response = $this->s3Client->getObject($params);
} catch (S3Exception $e) {
if (!$this->getThrowExceptions()) {
return false;
}
throw new \RuntimeException('Exception thrown by Aws\\S3\\S3Client: ' . $e->getMessage(), null, $e);
}
$body = $response->get('Body');
$body->rewind();
return $body->getStream();
}
示例12: fopen
public function fopen($path, $mode)
{
$path = $this->normalizePath($path);
switch ($mode) {
case 'r':
case 'rb':
$tmpFile = \OC_Helper::tmpFile();
self::$tmpFiles[$tmpFile] = $path;
try {
$result = $this->connection->getObject(array('Bucket' => $this->bucket, 'Key' => $this->cleanKey($path), 'SaveAs' => $tmpFile));
} catch (S3Exception $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
}
return fopen($tmpFile, 'r');
case 'w':
case 'wb':
case 'a':
case 'ab':
case 'r+':
case 'w+':
case 'wb+':
case 'a+':
case 'x':
case 'x+':
case 'c':
case 'c+':
if (strrpos($path, '.') !== false) {
$ext = substr($path, strrpos($path, '.'));
} else {
$ext = '';
}
$tmpFile = \OC_Helper::tmpFile($ext);
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) {
$source = $this->fopen($path, 'r');
file_put_contents($tmpFile, $source);
}
self::$tmpFiles[$tmpFile] = $path;
return fopen('close://' . $tmpFile, $mode);
}
return false;
}
示例13: streamBlob
/**
* @param string $container
* @param string $name
* @param array $params
*
* @throws DfException
*/
public function streamBlob($container, $name, $params = [])
{
try {
$this->checkConnection();
/** @var \Aws\Result $result */
$result = $this->blobConn->getObject(['Bucket' => $container, 'Key' => $name]);
header('Last-Modified: ' . $result->get('LastModified'));
header('Content-Type: ' . $result->get('ContentType'));
header('Content-Length:' . intval($result->get('ContentLength')));
$disposition = isset($params['disposition']) && !empty($params['disposition']) ? $params['disposition'] : 'inline';
header('Content-Disposition: ' . $disposition . '; filename="' . $name . '";');
echo $result->get('Body');
} catch (\Exception $ex) {
if ('Resource could not be accessed.' == $ex->getMessage()) {
$status_header = "HTTP/1.1 404 The specified file '{$name}' does not exist.";
header($status_header);
header('Content-Type: text/html');
} else {
throw new DfException('Failed to stream blob: ' . $ex->getMessage());
}
}
}
示例14: getFromS3
/**
* @brief Download a file from S3 to $path
*
* @param string $key "path" to a file on S3
* @param string $path Local path to save the file within app
* @param string $env environment variable, one of 'dev', 'test' or 'prod'
* @return void
*/
protected static function getFromS3(IOInterface $io, $key, $path, $env)
{
$bucket = 'keboola-configs';
if ($env == 'test') {
$bucket = 'keboola-configs-testing';
} elseif ($env == 'dev') {
$bucket = 'keboola-configs-devel';
}
if (getenv('KEBOOLA_SYRUP_CONFIGS_BUCKET')) {
$bucket = getenv('KEBOOLA_SYRUP_CONFIGS_BUCKET');
}
$awsRegion = 'us-east-1';
if (getenv('AWS_REGION')) {
$awsRegion = getenv('AWS_REGION');
}
$client = new S3Client(array('version' => '2006-03-01', 'region' => $awsRegion));
$client->getObject(array('Bucket' => $bucket, 'Key' => $key, 'SaveAs' => $path));
$io->write("<info>File <comment>{$path}</comment> downloaded from S3 ({$bucket})</info>");
}
示例15: read
/**
* Read a file
*
* @param string $path
* @return array file metadata
*/
public function read($path)
{
$options = $this->getOptions($path);
$result = $this->client->getObject($options);
return $this->normalizeObject($result->getAll(), $path);
}