Skip to content

Commit fd08878

Browse files
authored
Merge pull request #7 from boite/master
Help library users react better to Object Storage failures
2 parents fd1a097 + 8186251 commit fd08878

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

src/Adapter/S3Adapter.php

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ObjectStorage\Adapter;
44

5+
use Aws\Exception\AwsException;
6+
use Aws\S3\Exception\S3Exception;
57
use Aws\S3\S3Client;
68
use InvalidArgumentException;
79

@@ -106,35 +108,53 @@ public function setCannedAcl(string $cannedAcl)
106108

107109
public function setData($key, $data)
108110
{
109-
$this->s3client->putObject(
110-
[
111-
'Bucket' => $this->bucketname,
112-
'Key' => $this->prefix . $key,
113-
'Body' => $data,
114-
'ACL' => $this->cannedAcl,
115-
]
116-
);
111+
try {
112+
$this->s3client->putObject(
113+
[
114+
'Bucket' => $this->bucketname,
115+
'Key' => $this->prefix . $key,
116+
'Body' => $data,
117+
'ACL' => $this->cannedAcl,
118+
]
119+
);
120+
} catch (S3Exception $e) {
121+
throw new AdapterException('Failed to store data because of an S3 error', null, $e);
122+
} catch (AwsException $e) {
123+
throw new AdapterException('Failed to store data because of an AWS error', null, $e);
124+
}
117125
}
118126

119127
public function getData($key)
120128
{
121-
$result = $this->s3client->getObject(
122-
[
123-
'Bucket' => $this->bucketname,
124-
'Key' => $this->prefix . $key,
125-
]
126-
);
129+
try {
130+
$result = $this->s3client->getObject(
131+
[
132+
'Bucket' => $this->bucketname,
133+
'Key' => $this->prefix . $key,
134+
]
135+
);
136+
} catch (S3Exception $e) {
137+
throw new AdapterException('Failed to retrieve data because of an S3 error', null, $e);
138+
} catch (AwsException $e) {
139+
throw new AdapterException('Failed to retrieve data because of an AWS error', null, $e);
140+
}
127141

128142
return (string) $result['Body'];
129143
}
130144

131145
public function deleteData($key)
132146
{
133-
$this->s3client->deleteObject(
134-
[
135-
'Bucket' => $this->bucketname,
136-
'Key' => $this->prefix . $key,
137-
]
138-
);
147+
try {
148+
$this->s3client->deleteObject(
149+
[
150+
'Bucket' => $this->bucketname,
151+
'Key' => $this->prefix . $key,
152+
]
153+
);
154+
} catch (S3Exception $e) {
155+
throw new AdapterException('Failed to delete data because of an S3 error', null, $e);
156+
} catch (AwsException $e) {
157+
throw new AdapterException('Failed to delete data because of an AWS error', null, $e);
158+
}
139159
}
140160
}

0 commit comments

Comments
 (0)