Skip to content

Commit

Permalink
update code snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
ashika112 committed Aug 9, 2024
1 parent 51384ab commit dc6d016
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ Cross identity ID copying is only allowed if the destination path has the the ri

## Specify a bucket or copy across buckets / regions

Alternatively, choose a specific target by including `bucket` in both the source and destination objects.

See [additional storage bucket usage](/[platform]/build-a-backend/storage/set-up-storage/#additional-storage-bucket-usage) for more information.
You can also perform an `copy` operation to a specific bucket by providing the `bucket` option. This option can either be a string representing the target bucket's assigned name in Amplify Backend or an object specifying the bucket name and region from the console.

```javascript
import { copy } from 'aws-amplify/storage';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ This function does not check if the file exists by default. As result, the signe

</Callout>

The behavior of the `getUrl` API can be customized by passing an options object.
### More getUrl options

The behavior of the `getUrl` API can be customized by passing in options.

```typescript
import { getUrl } from 'aws-amplify/storage';

Expand All @@ -98,47 +101,14 @@ const linkToStorageFile = await getUrl({
// ensure object exists before getting url
validateObjectExistence: true,
// url expiration time in seconds.
expiresIn: 300, // 5 minutes
// use accelerate endpoint
useAccelerateEndpoint: true,
}
});
```
### Get url from a specified bucket

See [additional storage bucket usage](/[platform]/build-a-backend/storage/set-up-storage/#additional-storage-bucket-usage) for more information.

```ts
import { getUrl } from 'aws-amplify/storage';

const linkToStorageFile = await getUrl({
path: 'album/2024/1.jpg',
options: {
expiresIn: 300,
// Specify a target bucket using name assigned in Amplify Backend
bucket: 'assignedNameInAmplifyBackend'
}
});
```
```ts
import { getUrl } from 'aws-amplify/storage';

const result = await getUrl({
path: '/album/2024/1.jpg',
options: {
// Alternatively, provide bucket name from console and associated region
bucket: {
bucketName: 'bucket-name-from-console',
region: 'us-east-2'
}
bucket: 'assignedNameInAmplifyBackend',
useAccelerateEndpoint: true,
}
});


```


### More `getUrl` options

Option | Type | Default | Description |
| :--: | :--: | :--: | ----------- |
| bucket | string \| <br />\{ bucketName: string;<br/> region: string; \} | Bucket assigned `isDefault` | A string representing the target bucket's assigned name in Amplify Backend or an object specifying the bucket name and region from the console.<br/><br/>Read more at [Configure additional storage buckets](/[platform]/build-a-backend/storage/set-up-storage/#configure-additional-storage-buckets)
Expand Down Expand Up @@ -340,9 +310,8 @@ import { downloadData } from 'aws-amplify/storage';

// Downloads file content to memory
const { body, eTag } = await downloadData({
path: "/album/2024/1.jpg",
path: "album/2024/1.jpg"
}).result;

```
</InlineFilter>

Expand Down Expand Up @@ -539,7 +508,7 @@ import { downloadData } from 'aws-amplify/storage';

try {
const downloadResult = await downloadData({
path: "/album/2024/1.jpg"
path: "album/2024/1.jpg"
}).result;
const text = await downloadResult.body.text();
// Alternatively, you can use `downloadResult.body.blob()`
Expand Down Expand Up @@ -611,7 +580,7 @@ import { downloadData } from 'aws-amplify/storage';
// Download a file from S3 bucket
const { body, eTag } = await downloadData(
{
path: "/album/2024/1.jpg",
path: 'album/2024/1.jpg',
options: {
onProgress: (progress) {
console.log(`Download progress: ${(progress.transferredBytes/progress.totalBytes) * 100}%`);
Expand All @@ -626,7 +595,7 @@ const { body, eTag } = await downloadData(
```javascript
import { downloadData, isCancelError } from 'aws-amplify/storage';

const downloadTask = downloadData({ path: "/album/2024/1.jpg" });
const downloadTask = downloadData({ path: 'album/2024/1.jpg' });
downloadTask.cancel();
try {
await downloadTask.result;
Expand All @@ -639,25 +608,27 @@ try {

### Download from a specified bucket

See [additional storage bucket usage](/[platform]/build-a-backend/storage/set-up-storage/#additional-storage-bucket-usage) for more information.
You can also perform an upload operation to a specific bucket by providing the `bucket` option. You can pass in a string representing the target bucket's assigned name in Amplify Backend.
```ts
import { downloadData } from 'aws-amplify/storage';
const result = await downloadData({
path: '/album/2024/1.jpg',
path: 'album/2024/1.jpg',
options: {
// Specify a target bucket using name assigned in Amplify Backend
bucket: 'assignedNameInAmplifyBackend'
}
}).result;
```
Alternatively, you can also pass in an object by specifying the bucket name and region from the console.
```ts
import { downloadData } from 'aws-amplify/storage';
const result = await downloadData({
path: '/album/2024/1.jpg',
path: 'album/2024/1.jpg',
options: {
// Alternatively, provide bucket name from console and associated region
bucket: {
Expand Down Expand Up @@ -971,6 +942,25 @@ Download tasks are run using `URLSessionTask` instances internally. You can lear
<InlineFilter filters={["react", "angular", "javascript", "vue", "nextjs", "react-native"]}>

### More download options
The behavior of the `downloadData` API can be customized by passing in options.

```javascript
import { downloadData } from 'aws-amplify/storage';

// Downloads file content to memory
const { body, eTag } = await downloadData({
path: "album/2024/1.jpg",
options: {
// optional bytes range parameter to download a part of the file, the 2nd MB of the file in this example
bytesRange: {
start: 1024,
end: 2048
},
useAccelerateEndpoint: true,
}
}).result;

```

Option | Type | Default | Description |
| :--: | :--: | :--: | ----------- |
Expand Down
68 changes: 18 additions & 50 deletions src/pages/[platform]/build-a-backend/storage/list-files/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ You can list files without having to download all the files. You can do this by
import { list } from 'aws-amplify/storage';

const result = await list({
path: 'photos/',
path: 'album/photos/',
// Alternatively, path: ({identityId}) => `album/{identityId}/photos/`
});
```

Note the trailing slash `/` - if you had requested `list({ path : 'photos' })` it would also match against files like `photos123.jpg` alongside `photos/123.jpg`.
Note the trailing slash `/` - if you had requested `list({ path : 'album/photos' })` it would also match against files like `album/photos123.jpg` alongside `album/photos/123.jpg`.

The format of the response will look similar to the below example:

```js
{
items: [
{
path: "photos/123.jpg",
path: "album/photos/123.jpg",
eTag: "30074401292215403a42b0739f3b5262",
lastModified: "Thu Oct 08 2020 23:59:31 GMT+0800 (Singapore Standard Time)",
size: 138256
Expand Down Expand Up @@ -171,33 +171,26 @@ const result = await list({
```
### List files from a specified bucket
See [additional storage bucket usage](/[platform]/build-a-backend/storage/set-up-storage/#additional-storage-bucket-usage) for more information.
```ts
import { list } from 'aws-amplify/storage';
You can also perform an `copy` operation to a specific bucket by providing the `bucket` option. This option can either be a string representing the target bucket's assigned name in Amplify Backend or an object specifying the bucket name and region from the console.

const result = await list({
path: 'photos/',
options: {
// Specify a target bucket using name assigned in Amplify Backend
bucket: 'assignedNameInAmplifyBackend'
}
});
```
```ts
import { list } from 'aws-amplify/storage';
const result = await list({
path: 'photos/',
options: {
// Specify a target bucket using name assigned in Amplify Backend
bucket: 'assignedNameInAmplifyBackend',
// Alternatively, provide bucket name from console and associated region
bucket: {
bucketName: 'generated-secondary-bucket-name',
region: 'us-east-2'
}
// bucket: {
// bucketName: 'generated-secondary-bucket-name',
// region: 'us-east-2'
// }
}
});
```

### More `list` options

| Option | Type | Default | Description |
Expand Down Expand Up @@ -574,8 +567,12 @@ import { getProperties } from 'aws-amplify/storage';

try {
const result = await getProperties({
path: 'album/2024/1.jpg'
path: 'album/2024/1.jpg',
// Alternatively, path: ({ identityId }) => `album/{identityId}/1.jpg`
options: {
// Specify a target bucket using name assigned in Amplify Backend
bucket: 'assignedNameInAmplifyBackend'
}
});
console.log('File Properties ', result);
} catch (error) {
Expand All @@ -596,40 +593,11 @@ The properties and metadata will look similar to the below example
}
```

### Get file properties from a specified bucket
See [additional storage bucket usage](/[platform]/build-a-backend/storage/set-up-storage/#additional-storage-bucket-usage) for more information.
```ts
import { getProperties } from 'aws-amplify/storage';
const result = await getProperties({
path: 'album/2024/1.jpg',
options: {
// Specify a target bucket using name assigned in Amplify Backend
bucket: 'assignedNameInAmplifyBackend'
}
});
```
```ts
import { getProperties } from 'aws-amplify/storage';
const result = await getProperties({
path: 'album/2024/1.jpg',
options: {
// Alternatively, provide bucket name from console and associated region
bucket: {
bucketName: 'generated-secondary-bucket-name',
region: 'us-east-2'
}
}
});
```
### More `getProperties` options

Option | Type | Default | Description |
| -- | :--: | :--: | ----------- |
| bucket | string \| <br />\{ bucketName: string;<br/> region: string; \} | Bucket assigned `isDefault` | A string representing the target bucket's assigned name in Amplify Backend or an object specifying the bucket name and region from the console. |
| -- | -- | -- | ----------- |
| bucket | string \| <br />\{ bucketName: string;<br/> region: string; \} | undefined | A string representing the target bucket's assigned name in Amplify Backend or an object specifying the bucket name and region from the console.<br/><br/>Read more at [Configure additional storage buckets](/[platform]/build-a-backend/storage/set-up-storage/#configure-additional-storage-buckets) |
| useAccelerateEndpoint | boolean | false | Whether to use accelerate endpoint. | [Transfer Acceleration](/[platform]/build-a-backend/storage/extend-s3-resources/#example---enable-transfer-acceleration) |

<Callout>
Expand Down
36 changes: 16 additions & 20 deletions src/pages/[platform]/build-a-backend/storage/remove-files/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export function getStaticProps(context) {

Files can be removed from a storage bucket using the 'remove' API. If a file is protected by an identity Id, only the user who owns the file will be able to remove it.

You can also perform an upload operation to a specific bucket by providing the target bucket's assigned name from Amplify Backend in `bucket` option.

<InlineFilter filters={["react", "angular", "javascript", "vue", "nextjs", "react-native"]}>

```javascript
Expand All @@ -40,44 +42,38 @@ try {
await remove({
path: 'album/2024/1.jpg',
// Alternatively, path: ({identityId}) => `album/{identityId}/1.jpg`
bucket: 'assignedNameInAmplifyBackend', // Specify a target bucket using name assigned in Amplify Backend
});
} catch (error) {
console.log('Error ', error);
}
```
</InlineFilter>

## Remove file from specified bucket

See [additional storage bucket usage](/[platform]/build-a-backend/storage/set-up-storage/#additional-storage-bucket-usage) for more information.

```ts
import { remove } from 'aws-amplify/storage';
Alternatively, you can also pass in an object by specifying the bucket name and region from the console.

const result = await remove({
path: '/album/2024/1.jpg',
options: {
// Specify a target bucket using name assigned in Amplify Backend
bucket: 'assignedNameInAmplifyBackend'
}
});
```
<InlineFilter filters={["react", "angular", "javascript", "vue", "nextjs", "react-native"]}>

```ts
```javascript
import { remove } from 'aws-amplify/storage';

const result = await remove({
path: '/album/2024/1.jpg',
options: {
try {
await remove({
path: 'album/2024/1.jpg',
// Alternatively, provide bucket name from console and associated region
bucket: {
bucketName: 'bucket-name-from-console',
region: 'us-east-2'
}
}
});

});
} catch (error) {
console.log('Error ', error);
}
```
</InlineFilter>


<InlineFilter filters={["android"]}>
<BlockSwitcher>
<Block name="Java">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ const monitorUpload = async () => {
try {
const result = await uploadData({
path: "album/2024/1.jpg",
// Alternatively, path: ({identityId}) => `album/${identityId}/1.jpg`
data: file,
options: {
onProgress: ({ transferredBytes, totalBytes }) => {
Expand Down Expand Up @@ -1200,6 +1201,8 @@ func getTempUrls(securityScopedUrls: [URL]) -> [URL] {

### Upload to a specified bucket

You can also perform an upload operation to a specific bucket by providing the `bucket` option. You can pass in a string representing the target bucket's assigned name in Amplify Backend.

```ts
import { uploadData } from 'aws-amplify/storage';

Expand All @@ -1212,6 +1215,8 @@ const result = await uploadData({
}
}).result;
```
Alternatively, you can also pass in an object by specifying the bucket name and region from the console.

```ts
import { uploadData } from 'aws-amplify/storage';

Expand All @@ -1228,7 +1233,6 @@ const result = await uploadData({
}).result;

```

</InlineFilter>

<InlineFilter filters={["react", "angular", "javascript", "vue", "nextjs", "react-native"]}>
Expand Down

0 comments on commit dc6d016

Please sign in to comment.