Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] - AWS PHP SDK examples #328

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 69 additions & 0 deletions app/dcs/code/aws/php/page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: AWS SDK PHP
docId: LuaGgrbZ9rKbWtDMXhFWG
---

## 1. Install or include the Amazon S3 SDK

e.g. with composer

```none
composer require aws/aws-sdk-php
```

### 2. Import the S3 Client

```php
use Aws\S3\S3Client;
```

### 3. Create S3 Client

```php
$s3 = new S3Client([
'version' => 'latest',
'endpoint' => 'https://gateway.storjshare.io',
'credentials' => [
'key' => 'your_key',
'secret' => 'your_secret'
],
]);
```

### 4. List of objects

```php
$results = $s3->getPaginator('ListObjects', [
'Bucket' => 'your_bucket'
]
);

foreach ($results as $result) {
foreach ($result['Contents'] as $object) {
echo $object['Key'] . PHP_EOL;
}
}
```

### 5. Get one object

```php
$result = $s3->getObject([
'Bucket' => 'your_bucket_name',
'Key' => $id, // Name of object e.g. image.png
]);

header('Content-Type: ' . $result['ContentType']);
echo $result['Body'];
```

### 6. Upload an object

```php
$s3->putObject([
'Bucket' => 'your_bucket',
'Key' => $key,
'Body' => $image_base64,
'ContentType' => $content_type,
]);
```