Skip to content

Commit 39653d8

Browse files
committed
docs/sdks/s3/js: move examples into separate files
The examples have also been amended to explain themselves better and have all been tested to ensure they work.
1 parent 5755ae5 commit 39653d8

File tree

8 files changed

+1983
-105
lines changed

8 files changed

+1983
-105
lines changed

docs/sdks/s3/aws-go-sdk.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ListObjectsV2, and GetObject.
2020

2121
import gettingStarted from "!!raw-loader!../../../examples/go/cmd/getting-started/main.go";
2222

23-
<CodeBlock language="go">{gettingStarted}</CodeBlock>;
23+
<CodeBlock language="go">{gettingStarted}</CodeBlock>
2424

2525
Run it with:
2626

@@ -38,7 +38,7 @@ since it was read.
3838

3939
import conditionalOperations from "!!raw-loader!../../../examples/go/cmd/conditional-operations/main.go";
4040

41-
<CodeBlock language="go">{conditionalOperations}</CodeBlock>;
41+
<CodeBlock language="go">{conditionalOperations}</CodeBlock>
4242

4343
Run it with:
4444

@@ -52,7 +52,7 @@ Presigned URLs can be used with the AWS Go SDK as follows:
5252

5353
import presignedUrls from "!!raw-loader!../../../examples/go/cmd/presigned-urls/main.go";
5454

55-
<CodeBlock language="go">{presignedUrls}</CodeBlock>;
55+
<CodeBlock language="go">{presignedUrls}</CodeBlock>
5656

5757
You can now use the URL returned by the `presignedPutReq.URL` and
5858
`presignedGetReq.URL` to upload or download objects.
@@ -70,7 +70,7 @@ Below is an example of how to use the AWS Go SDK to restrict
7070

7171
import objectRegions from "!!raw-loader!../../../examples/go/cmd/object-regions/main.go";
7272

73-
<CodeBlock language="go">{objectRegions}</CodeBlock>;
73+
<CodeBlock language="go">{objectRegions}</CodeBlock>
7474

7575
Run it with:
7676

@@ -85,7 +85,7 @@ Below is an example of how to use
8585

8686
import metadataQuerying from "!!raw-loader!../../../examples/go/cmd/metadata-querying/main.go";
8787

88-
<CodeBlock language="go">{metadataQuerying}</CodeBlock>;
88+
<CodeBlock language="go">{metadataQuerying}</CodeBlock>
8989

9090
## Object Notifications
9191

@@ -96,4 +96,4 @@ so you can take action when an object is created or deleted.
9696

9797
import webhook from "!!raw-loader!../../../examples/go/cmd/webhook/main.go";
9898

99-
<CodeBlock language="go">{webhook}</CodeBlock>;
99+
<CodeBlock language="go">{webhook}</CodeBlock>

docs/sdks/s3/aws-js-sdk.md

Lines changed: 0 additions & 99 deletions
This file was deleted.

docs/sdks/s3/aws-js-sdk.mdx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# AWS JavaScript SDK
2+
3+
import CodeBlock from "@theme/CodeBlock";
4+
5+
This guide assumes that you have followed the steps in the
6+
[Getting Started](/docs/get-started/index.md) guide, and have the access keys
7+
available.
8+
9+
You may continue to use the AWS JS SDK as you normally would, but with the
10+
endpoint set to `https://fly.storage.tigris.dev`.
11+
12+
## Getting started
13+
14+
This example uses the
15+
[AWS Node.js SDK v3](https://www.npmjs.com/package/@aws-sdk/client-s3) and reads
16+
the default credentials file or the environment variables `AWS_ACCESS_KEY_ID`
17+
and `AWS_SECRET_ACCESS_KEY`.
18+
19+
import gettingStarted from "!!raw-loader!../../../examples/js/getting-started.js";
20+
21+
<CodeBlock language="js">{gettingStarted}</CodeBlock>
22+
23+
## Using presigned URLs
24+
25+
Presigned URLs can be used with the AWS Node.js SDK as follows:
26+
27+
import presignedUrls from "!!raw-loader!../../../examples/js/presigned-urls.js";
28+
29+
<CodeBlock language="js">{presignedUrls}</CodeBlock>
30+
31+
You can now use the URL returned by the `getSignedUrl` function to download or
32+
upload objects to the bucket using the `https` package.

examples/js/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

examples/js/getting-started.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {
2+
S3Client,
3+
paginateListBuckets,
4+
paginateListObjectsV2,
5+
PutObjectCommand,
6+
} from "@aws-sdk/client-s3";
7+
import { readFile } from "node:fs/promises";
8+
9+
// listBuckets returns a list of all S3 buckets in the account with
10+
// metadata such as creation date and owner.
11+
export const listBuckets = async (S3) => {
12+
const buckets = [];
13+
for await (const page of paginateListBuckets({ client: S3 }, {})) {
14+
if (page.Buckets) {
15+
buckets.push(...page.Buckets);
16+
}
17+
}
18+
return buckets;
19+
};
20+
21+
// listObjects returns a list of all objects in a bucket. This only returns
22+
// the keys of the objects, not the objects themselves. Customize the
23+
// objects.push line to return more metadata about the objects.
24+
export const listObjects = async (S3, bucketName) => {
25+
const paginator = paginateListObjectsV2(
26+
{ client: S3, pageSize: 100 },
27+
{ Bucket: bucketName }
28+
);
29+
const objects = [];
30+
31+
for await (const page of paginator) {
32+
if (page.Contents) {
33+
objects.push(page.Contents.map((o) => o.Key)); // only get object keys
34+
}
35+
}
36+
return objects;
37+
};
38+
39+
// uploadObjectFromFS uploads a file from the local filesystem to an S3 bucket.
40+
// This does not handle large files or multipart uploads.
41+
export const uploadObjectFromFS = async (S3, bucket, key, filePath) => {
42+
const command = new PutObjectCommand({
43+
Bucket: bucket,
44+
Key: key,
45+
Body: await readFile(filePath),
46+
});
47+
48+
const response = await S3.send(command);
49+
return response;
50+
};
51+
52+
const S3 = new S3Client({ region: "auto" });
53+
54+
console.log("List buckets");
55+
const buckets = await listBuckets(S3);
56+
console.log("Buckets:", buckets);
57+
58+
console.log("List objects in a bucket");
59+
const objects = await listObjects(S3, "tigris-example");
60+
objects.forEach((objects, pageNum) => {
61+
console.log(`Page ${pageNum + 1}:`, objects);
62+
});
63+
64+
console.log("Upload an object");
65+
const response = await uploadObjectFromFS(
66+
S3,
67+
"tigris-example",
68+
"examples/js/getting-started.js",
69+
"getting-started.js"
70+
);
71+
console.log("Upload response:", response);

0 commit comments

Comments
 (0)