/** * Return the web-accessible uri for the given file. * * @param string $file * @return string The file uri * @throws \RuntimeException */ public function uri($file) { if (null !== $this->filenameFilter) { $file = $this->filenameFilter->filter($file); } try { $request = $this->s3Client->get($this->getBucket() . '/' . $file); return $this->s3Client->getPresignedUrl($request, $this->getUriExpirationTime()); } catch (S3Exception $e) { if (!$this->getThrowExceptions()) { return null; } throw new \RuntimeException('Exception thrown by Aws\\S3\\S3Client: ' . $e->getMessage(), null, $e); } }
/** * @depends testPutAndListObjects */ public function testPreSignedUrlAllowsSpecialCharacters() { self::log('Uploading an object with a space in the key'); $this->client->waitUntil('bucket_exists', array('Bucket' => $this->bucket)); $key = 'foo baz bar!'; $this->client->putObject(array('Bucket' => $this->bucket, 'Key' => $key, 'Body' => 'hi')); $this->client->waitUntil('object_exists', array('Bucket' => $this->bucket, 'Key' => $key)); self::log('Creating an downloading using a pre-signed URL'); $extra = urlencode("attachment; filename=\"{$key}\""); $request = $this->client->get("{$this->bucket}/{$key}?response-content-disposition={$extra}"); $url = $this->client->createPresignedUrl($request, '+10 minutes'); self::log($url); $client = new Client(); $this->assertEquals('hi', file_get_contents($url)); $this->assertEquals('hi', $client->get($url)->send()->getBody(true)); }