Files
supabase/apps/temp-docs/docs/reference/dart/storage-from-upload.mdx
2022-04-26 14:17:19 +02:00

152 lines
3.2 KiB
Plaintext

---
id: storage-from-upload
title: "from.upload()"
slug: storage-from-upload
custom_edit_url: https://github.com/supabase/supabase/edit/master/web/spec/dart.yml
---
import Tabs from '@theme/Tabs';
import TabsPanel from '@theme/TabsPanel';
Uploads a file to an existing bucket.
<Tabs
defaultActiveId="dart"
groupId="reference/dart"
values={[{ label: 'Dart', value: 'dart' }]}>
<TabsPanel id="dart" label="dart">
```dart
final avatarFile = File('path/to/file');
final res = await supabase
.storage
.from('avatars')
.upload('public/avatar1.png', avatarFile, fileOptions: FileOptions(
cacheControl: '3600',
upsert: false
));
```
</TabsPanel>
</Tabs>
## Parameters
<ul className="method-list-group">
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
path
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
<code>string</code>
</span>
</h4>
<div class="method-list-item-description">
The relative file path. Should be of the format `folder/subfolder/filename.png`. The bucket must already exist before attempting to upload.
</div>
</li>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
fileBody
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
<code>ArrayBuffer</code> | <code>ArrayBufferView</code> | <code>Blob</code> | <code>Buffer</code> | <code>File</code> | <code>FormData</code> | <code>ReadableStream</code> | <code>ReadableStream</code> | <code>URLSearchParams</code> | <code>string</code>
</span>
</h4>
<div class="method-list-item-description">
The body of the file to be stored in the bucket.
</div>
</li>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
fileOptions
</span>
<span className="method-list-item-label-badge false">
optional
</span>
<span className="method-list-item-validation">
<code>FileOptions</code>
</span>
</h4>
<div class="method-list-item-description">
HTTP headers.
`cacheControl`: string, the `Cache-Control: max-age=<seconds>` seconds value.
`contentType`: string, the `Content-Type` header value. Should be specified if using a `fileBody` that is neither `Blob` nor `File` nor `FormData`, otherwise will default to `text/plain;charset=UTF-8`.
`upsert`: boolean, whether to perform an upsert.
</div>
</li>
</ul>
## Notes
- Policy permissions required:
- `buckets` permissions: none
- `objects` permissions: `insert`
## Examples
### Upload file
<Tabs
defaultActiveId="dart"
groupId="reference/dart"
values={[{ label: 'Dart', value: 'dart' }]}>
<TabsPanel id="dart" label="dart">
```dart
final avatarFile = File('path/to/file');
final res = await supabase
.storage
.from('avatars')
.upload('public/avatar1.png', avatarFile, fileOptions: FileOptions(
cacheControl: '3600',
upsert: false
));
```
</TabsPanel>
</Tabs>