Skip to content

Commit

Permalink
Merge pull request #17 from appwrite/feat-haimantika-docs-g2
Browse files Browse the repository at this point in the history
Added more methods to upload and download
  • Loading branch information
Vincent (Wen Yu) Ge authored Sep 22, 2023
2 parents f03b472 + 2df52b7 commit 729726a
Showing 1 changed file with 321 additions and 2 deletions.
323 changes: 321 additions & 2 deletions src/routes/docs/products/storage/upload-download/+page.markdoc
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ The Appwrite Python SDK expects an `InputFile` class for file inputs.
| ------------------------------------------ | ------------------------------------------------------------ |
| `InputFile.from_path(path)` | Used to upload files from a provided path. |
| `InputFile.from_bytes(bytes)` | Used to upload files from an array of bytes. |
{% /tabsitem %}
{% /tabsitem %}

{% tabsitem #ruby title="Ruby" %}
The Appwrite Ruby SDK expects an `InputFile` class for file inputs.
Expand Down Expand Up @@ -277,7 +277,114 @@ The Appwrite .NET SDK expects an `InputFile` class for file inputs.
{% /tabsitem %}
{% /tabs %}

[TODO more download examples]
## Get file {% #get-file %}
To get a metadata about a.file, use the `getFile` method.

{% multicode %}
```js
import { Client, Storage } from "appwrite";

const client = new Client();

const storage = new Storage(client);

client
.setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
.setProject('5df5acd0d48c2') // Your project ID
;

const promise = storage.getFile('[BUCKET_ID]', '[FILE_ID]');

promise.then(function (response) {
console.log(response); // Success
}, function (error) {
console.log(error); // Failure
});
```
```dart
import 'package:appwrite/appwrite.dart';

void main() { // Init SDK
Client client = Client();
Storage storage = Storage(client);

client
.setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
.setProject('5df5acd0d48c2') // Your project ID
;
// downloading file
Future result = storage.getFile(
bucketId: '[BUCKET_ID]',
fileId: '[FILE_ID]',
).then((bytes) {
final file = File('path_to_file/filename.ext');
file.writeAsBytesSync(bytes)
}).catchError((error) {
print(error.response);
})
}

//displaying image preview
FutureBuilder(
future: storage.getFile(
bucketId: '[BUCKET_ID]',
fileId: '[FILE_ID]',
), //works for both public file and private file, for private files you need to be logged in
builder: (context, snapshot) {
return snapshot.hasData && snapshot.data != null
? Image.memory(
snapshot.data,
)
: CircularProgressIndicator();
},
);
```
```swift
import Appwrite

func main() async throws {
let client = Client()
.setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID
let storage = Storage(client)
let byteBuffer = try await storage.getFile(
bucketId: "[BUCKET_ID]",
fileId: "[FILE_ID]"
)

print(String(describing: byteBuffer)
}
```
```kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import io.appwrite.Client
import io.appwrite.services.Storage

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val client = Client(applicationContext)
.setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID

val storage = Storage(client)

GlobalScope.launch {
val result = storage.getFile(
bucketId = "[BUCKET_ID]",
fileId = "[FILE_ID]"
)
println(result); // Resource URL
}
}
}
```
{% /multicode %}

## Download file {% #download-file %}
To download a file, use the `getFileDownload` method.
Expand Down Expand Up @@ -382,4 +489,216 @@ class MainActivity : AppCompatActivity() {
}
}
```
{% /multicode %}

## View file {% #view-file%}

To view a file, use the `getFileView` method.

{% multicode %}
```js
import { Client, Storage } from "appwrite";

const client = new Client();

const storage = new Storage(client);

client
.setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
.setProject('5df5acd0d48c2') // Your project ID
;

const result = storage.getFileView('[BUCKET_ID]', '[FILE_ID]');

console.log(result); // Resource URL
```
```dart
import 'package:appwrite/appwrite.dart';

void main() { // Init SDK
Client client = Client();
Storage storage = Storage(client);

client
.setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
.setProject('5df5acd0d48c2') // Your project ID
;
// downloading file
Future result = storage.getFileView(
bucketId: '[BUCKET_ID]',
fileId: '[FILE_ID]',
).then((bytes) {
final file = File('path_to_file/filename.ext');
file.writeAsBytesSync(bytes)
}).catchError((error) {
print(error.response);
})
}

//displaying image preview
FutureBuilder(
future: storage.getFileView(
bucketId: '[BUCKET_ID]',
fileId: '[FILE_ID]',
), //works for both public file and private file, for private files you need to be logged in
builder: (context, snapshot) {
return snapshot.hasData && snapshot.data != null
? Image.memory(
snapshot.data,
)
: CircularProgressIndicator();
},
);
```
```swift
import Appwrite

func main() async throws {
let client = Client()
.setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID
let storage = Storage(client)
let byteBuffer = try await storage.getFileView(
bucketId: "[BUCKET_ID]",
fileId: "[FILE_ID]"
)

print(String(describing: byteBuffer)
}
```
```kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import io.appwrite.Client
import io.appwrite.services.Storage

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val client = Client(applicationContext)
.setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID

val storage = Storage(client)

GlobalScope.launch {
val result = storage.getFileView(
bucketId = "[BUCKET_ID]",
fileId = "[FILE_ID]"
)
println(result); // Resource URL
}
}
}
```
{% /multicode %}

## Get file preview {% #get-file-preview %}
To get a file preview image , use the `getFilePreview` method.
You can learn more about the **image manupulation options** in [this guide](/docs/products/storage/images).

{% multicode %}
```js
import { Client, Storage } from "appwrite";

const client = new Client();

const storage = new Storage(client);

client
.setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
.setProject('5df5acd0d48c2') // Your project ID
;

const result = storage.getFilePreview('[BUCKET_ID]', '[FILE_ID]');

console.log(result); // Resource URL
```
```dart
import 'package:appwrite/appwrite.dart';

void main() { // Init SDK
Client client = Client();
Storage storage = Storage(client);

client
.setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
.setProject('5df5acd0d48c2') // Your project ID
;
// downloading file
Future result = storage.getFilePreview(
bucketId: '[BUCKET_ID]',
fileId: '[FILE_ID]',
).then((bytes) {
final file = File('path_to_file/filename.ext');
file.writeAsBytesSync(bytes)
}).catchError((error) {
print(error.response);
})
}

//displaying image preview
FutureBuilder(
future: storage.getFilePreview(
bucketId: '[BUCKET_ID]',
fileId: '[FILE_ID]',
), //works for both public file and private file, for private files you need to be logged in
builder: (context, snapshot) {
return snapshot.hasData && snapshot.data != null
? Image.memory(
snapshot.data,
)
: CircularProgressIndicator();
},
);
```
```swift
import Appwrite

func main() async throws {
let client = Client()
.setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID
let storage = Storage(client)
let byteBuffer = try await storage.getFilePreview(
bucketId: "[BUCKET_ID]",
fileId: "[FILE_ID]"
)

print(String(describing: byteBuffer)
}
```
```kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import io.appwrite.Client
import io.appwrite.services.Storage

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val client = Client(applicationContext)
.setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID

val storage = Storage(client)

GlobalScope.launch {
val result = storage.getFilePreview(
bucketId = "[BUCKET_ID]",
fileId = "[FILE_ID]"
)
println(result); // Resource URL
}
}
}
```
{% /multicode %}

0 comments on commit 729726a

Please sign in to comment.