public function __invoke(CommandInterface $command, RequestInterface $request)
 {
     $name = $command->getName();
     $body = $request->getBody();
     if (!$request->hasHeader('Content-MD5') && $body->getSize() && in_array($name, self::$requireMd5)) {
         $request = $request->withHeader('Content-MD5', base64_encode(Psr7\hash($body, 'md5', true)));
     }
     $next = $this->nextHandler;
     return $next($command, $request);
 }
 public function __invoke(CommandInterface $command, ResponseInterface $response)
 {
     if ($expected = $response->getHeaderLine('x-amz-crc32')) {
         $hash = hexdec(Psr7\hash($response->getBody(), 'crc32b'));
         if ((int) $expected !== $hash) {
             throw new AwsException("crc32 mismatch. Expected {$expected}, found {$hash}.", $command, ['code' => 'ClientChecksumMismatch', 'connection_error' => true, 'response' => $response]);
         }
     }
     $fn = $this->parser;
     return $fn($command, $response);
 }
 public function __invoke(CommandInterface $command, RequestInterface $request)
 {
     $next = $this->nextHandler;
     $name = $command->getName();
     $body = $request->getBody();
     if (in_array($name, self::$md5) && !$request->hasHeader('Content-MD5')) {
         // Set the content MD5 header for operations that require it.
         $request = $request->withHeader('Content-MD5', base64_encode(Psr7\hash($body, 'md5', true)));
     } elseif (in_array($name, self::$sha256) && $command['ContentSHA256']) {
         // Set the content hash header if provided in the parameters.
         $request = $request->withHeader('X-Amz-Content-Sha256', $command['ContentSHA256']);
     }
     return $next($command, $request);
 }
Esempio n. 4
0
 protected function getPayload(RequestInterface $request)
 {
     // Calculate the request signature payload
     if ($request->hasHeader('X-Amz-Content-Sha256')) {
         // Handle streaming operations (e.g. Glacier.UploadArchive)
         return $request->getHeaderLine('X-Amz-Content-Sha256');
     }
     if (!$request->getBody()->isSeekable()) {
         throw new CouldNotCreateChecksumException('sha256');
     }
     try {
         return Psr7\hash($request->getBody(), 'sha256');
     } catch (\Exception $e) {
         throw new CouldNotCreateChecksumException('sha256', $e);
     }
 }
Esempio n. 5
0
 public function testCalculatesHashSeeksToOriginalPosition()
 {
     $s = Psr7\stream_for('foobazbar');
     $s->seek(4);
     $this->assertEquals(md5('foobazbar'), Psr7\hash($s, 'md5'));
     $this->assertEquals(4, $s->tell());
 }
Esempio n. 6
0
 /**
  * @param array $args
  */
 private function resolveUploadOptions(array $args)
 {
     $args += ['bucket' => null, 'name' => null, 'validate' => true, 'resumable' => null, 'predefinedAcl' => 'private', 'metadata' => []];
     $args['data'] = Psr7\stream_for($args['data']);
     if ($args['resumable'] === null) {
         $args['resumable'] = $args['data']->getSize() > AbstractUploader::RESUMABLE_LIMIT;
     }
     if (!$args['name']) {
         $args['name'] = basename($args['data']->getMetadata('uri'));
     }
     // @todo add support for rolling hash
     if ($args['validate'] && !isset($args['metadata']['md5Hash'])) {
         $args['metadata']['md5Hash'] = base64_encode(Psr7\hash($args['data'], 'md5', true));
     }
     $args['metadata']['name'] = $args['name'];
     unset($args['name']);
     $args['contentType'] = isset($args['metadata']['contentType']) ? $args['metadata']['contentType'] : Psr7\mimetype_from_filename($args['metadata']['name']);
     $uploaderOptionKeys = ['httpOptions', 'retries', 'chunkSize', 'contentType', 'metadata'];
     $args['uploaderOptions'] = array_intersect_key($args, array_flip($uploaderOptionKeys));
     $args = array_diff_key($args, array_flip($uploaderOptionKeys));
     return $args;
 }
 public function insertObjectProvider()
 {
     $tempFile = Psr7\stream_for(fopen('php://temp', 'r+'));
     $tempFile->write(str_repeat('0', 5000001));
     $logoFile = Psr7\stream_for(fopen(__DIR__ . '../../../data/logo.svg', 'r'));
     return [[['data' => $tempFile, 'name' => 'file.txt', 'predefinedAcl' => 'private', 'metadata' => ['contentType' => 'text/plain']], 'Google\\Cloud\\Upload\\ResumableUploader', 'text/plain', ['md5Hash' => base64_encode(Psr7\hash($tempFile, 'md5', true)), 'name' => 'file.txt']], [['data' => $logoFile, 'validate' => false], 'Google\\Cloud\\Upload\\MultipartUploader', 'image/svg+xml', ['name' => 'logo.svg']], [['data' => 'abcdefg', 'name' => 'file.ext', 'resumable' => true, 'validate' => false, 'metadata' => ['contentType' => 'text/plain', 'metadata' => ['here' => 'wego']]], 'Google\\Cloud\\Upload\\ResumableUploader', 'text/plain', ['name' => 'file.ext', 'metadata' => ['here' => 'wego']]]];
 }