From ea11598628fcaae7b76f5ccbe212d9ad5912c638 Mon Sep 17 00:00:00 2001 From: haimantika mitra <32809211+Haimantika@users.noreply.github.com> Date: Tue, 19 Sep 2023 10:56:11 +0000 Subject: [PATCH 1/3] Added more methods to upload and download --- .../storage/upload-download/+page.markdoc | 325 +++++++++++++++++- 1 file changed, 324 insertions(+), 1 deletion(-) diff --git a/src/routes/docs/products/storage/upload-download/+page.markdoc b/src/routes/docs/products/storage/upload-download/+page.markdoc index 9adb3c8c67..93634cca58 100644 --- a/src/routes/docs/products/storage/upload-download/+page.markdoc +++ b/src/routes/docs/products/storage/upload-download/+page.markdoc @@ -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. @@ -383,4 +383,327 @@ class MainActivity : AppCompatActivity() { } ``` ```dart +{% /multicode %} + +## Get File {% #get-file %} +To get 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 + } + } +} +``` +```dart +{% /multicode %} + +## Get File Preview {% #get-file-preview %} +To get a file preview image , use the `getFilePreview` 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.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 + } + } +} +``` +```dart +{% /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 + } + } +} +``` +```dart {% /multicode %} \ No newline at end of file From d3b5b43459a9d53b7b2ccdd1302a5dbdc09448cd Mon Sep 17 00:00:00 2001 From: haimantika mitra <32809211+Haimantika@users.noreply.github.com> Date: Tue, 19 Sep 2023 20:00:45 +0530 Subject: [PATCH 2/3] Made changes --- src/routes/docs/products/storage/upload-download/+page.markdoc | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/routes/docs/products/storage/upload-download/+page.markdoc b/src/routes/docs/products/storage/upload-download/+page.markdoc index 93634cca58..0be75dc8f0 100644 --- a/src/routes/docs/products/storage/upload-download/+page.markdoc +++ b/src/routes/docs/products/storage/upload-download/+page.markdoc @@ -383,7 +383,6 @@ class MainActivity : AppCompatActivity() { } ``` ```dart -{% /multicode %} ## Get File {% #get-file %} To get a file, use the `getFile` method. @@ -705,5 +704,4 @@ class MainActivity : AppCompatActivity() { } } ``` -```dart {% /multicode %} \ No newline at end of file From da2e00e9b7c0c28a53db7b9753999742e006f3f5 Mon Sep 17 00:00:00 2001 From: "Vincent (Wen Yu) Ge" Date: Fri, 22 Sep 2023 15:28:17 +0000 Subject: [PATCH 3/3] Fix formatting --- .../storage/upload-download/+page.markdoc | 78 +++++++++---------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/src/routes/docs/products/storage/upload-download/+page.markdoc b/src/routes/docs/products/storage/upload-download/+page.markdoc index 0be75dc8f0..7417c537b3 100644 --- a/src/routes/docs/products/storage/upload-download/+page.markdoc +++ b/src/routes/docs/products/storage/upload-download/+page.markdoc @@ -277,10 +277,8 @@ The Appwrite .NET SDK expects an `InputFile` class for file inputs. {% /tabsitem %} {% /tabs %} -[TODO more download examples] - -## Download file {% #download-file %} -To download a file, use the `getFileDownload` method. +## Get file {% #get-file %} +To get a metadata about a.file, use the `getFile` method. {% multicode %} ```js @@ -295,9 +293,13 @@ client .setProject('5df5acd0d48c2') // Your project ID ; -const result = storage.getFileDownload('[BUCKET_ID]', '[FILE_ID]'); +const promise = storage.getFile('[BUCKET_ID]', '[FILE_ID]'); -console.log(result); // Resource URL +promise.then(function (response) { + console.log(response); // Success +}, function (error) { + console.log(error); // Failure +}); ``` ```dart import 'package:appwrite/appwrite.dart'; @@ -311,7 +313,7 @@ void main() { // Init SDK .setProject('5df5acd0d48c2') // Your project ID ; // downloading file - Future result = storage.getFileDownload( + Future result = storage.getFile( bucketId: '[BUCKET_ID]', fileId: '[FILE_ID]', ).then((bytes) { @@ -324,7 +326,7 @@ void main() { // Init SDK //displaying image preview FutureBuilder( - future: storage.getFileDownload( + 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 @@ -345,7 +347,7 @@ func main() async throws { .setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint .setProject("5df5acd0d48c2") // Your project ID let storage = Storage(client) - let byteBuffer = try await storage.getFileDownload( + let byteBuffer = try await storage.getFile( bucketId: "[BUCKET_ID]", fileId: "[FILE_ID]" ) @@ -373,7 +375,7 @@ class MainActivity : AppCompatActivity() { val storage = Storage(client) GlobalScope.launch { - val result = storage.getFileDownload( + val result = storage.getFile( bucketId = "[BUCKET_ID]", fileId = "[FILE_ID]" ) @@ -382,10 +384,10 @@ class MainActivity : AppCompatActivity() { } } ``` -```dart +{% /multicode %} -## Get File {% #get-file %} -To get a file, use the `getFile` method. +## Download file {% #download-file %} +To download a file, use the `getFileDownload` method. {% multicode %} ```js @@ -400,13 +402,9 @@ client .setProject('5df5acd0d48c2') // Your project ID ; -const promise = storage.getFile('[BUCKET_ID]', '[FILE_ID]'); +const result = storage.getFileDownload('[BUCKET_ID]', '[FILE_ID]'); -promise.then(function (response) { - console.log(response); // Success -}, function (error) { - console.log(error); // Failure -}); +console.log(result); // Resource URL ``` ```dart import 'package:appwrite/appwrite.dart'; @@ -420,7 +418,7 @@ void main() { // Init SDK .setProject('5df5acd0d48c2') // Your project ID ; // downloading file - Future result = storage.getFile( + Future result = storage.getFileDownload( bucketId: '[BUCKET_ID]', fileId: '[FILE_ID]', ).then((bytes) { @@ -433,7 +431,7 @@ void main() { // Init SDK //displaying image preview FutureBuilder( - future: storage.getFile( + future: storage.getFileDownload( bucketId: '[BUCKET_ID]', fileId: '[FILE_ID]', ), //works for both public file and private file, for private files you need to be logged in @@ -454,7 +452,7 @@ func main() async throws { .setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint .setProject("5df5acd0d48c2") // Your project ID let storage = Storage(client) - let byteBuffer = try await storage.getFile( + let byteBuffer = try await storage.getFileDownload( bucketId: "[BUCKET_ID]", fileId: "[FILE_ID]" ) @@ -482,7 +480,7 @@ class MainActivity : AppCompatActivity() { val storage = Storage(client) GlobalScope.launch { - val result = storage.getFile( + val result = storage.getFileDownload( bucketId = "[BUCKET_ID]", fileId = "[FILE_ID]" ) @@ -491,11 +489,11 @@ class MainActivity : AppCompatActivity() { } } ``` -```dart {% /multicode %} -## Get File Preview {% #get-file-preview %} -To get a file preview image , use the `getFilePreview` method. +## View file {% #view-file%} + +To view a file, use the `getFileView` method. {% multicode %} ```js @@ -510,7 +508,7 @@ client .setProject('5df5acd0d48c2') // Your project ID ; -const result = storage.getFilePreview('[BUCKET_ID]', '[FILE_ID]'); +const result = storage.getFileView('[BUCKET_ID]', '[FILE_ID]'); console.log(result); // Resource URL ``` @@ -526,7 +524,7 @@ void main() { // Init SDK .setProject('5df5acd0d48c2') // Your project ID ; // downloading file - Future result = storage.getFilePreview( + Future result = storage.getFileView( bucketId: '[BUCKET_ID]', fileId: '[FILE_ID]', ).then((bytes) { @@ -539,7 +537,7 @@ void main() { // Init SDK //displaying image preview FutureBuilder( - future: storage.getFilePreview( + 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 @@ -560,7 +558,7 @@ func main() async throws { .setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint .setProject("5df5acd0d48c2") // Your project ID let storage = Storage(client) - let byteBuffer = try await storage.getFilePreview( + let byteBuffer = try await storage.getFileView( bucketId: "[BUCKET_ID]", fileId: "[FILE_ID]" ) @@ -588,7 +586,7 @@ class MainActivity : AppCompatActivity() { val storage = Storage(client) GlobalScope.launch { - val result = storage.getFilePreview( + val result = storage.getFileView( bucketId = "[BUCKET_ID]", fileId = "[FILE_ID]" ) @@ -597,12 +595,11 @@ class MainActivity : AppCompatActivity() { } } ``` -```dart {% /multicode %} -## View File {% #view-file%} - -To view a file, use the `getFileView` method. +## 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 @@ -617,7 +614,7 @@ client .setProject('5df5acd0d48c2') // Your project ID ; -const result = storage.getFileView('[BUCKET_ID]', '[FILE_ID]'); +const result = storage.getFilePreview('[BUCKET_ID]', '[FILE_ID]'); console.log(result); // Resource URL ``` @@ -633,7 +630,7 @@ void main() { // Init SDK .setProject('5df5acd0d48c2') // Your project ID ; // downloading file - Future result = storage.getFileView( + Future result = storage.getFilePreview( bucketId: '[BUCKET_ID]', fileId: '[FILE_ID]', ).then((bytes) { @@ -646,7 +643,7 @@ void main() { // Init SDK //displaying image preview FutureBuilder( - future: storage.getFileView( + 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 @@ -667,7 +664,7 @@ func main() async throws { .setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint .setProject("5df5acd0d48c2") // Your project ID let storage = Storage(client) - let byteBuffer = try await storage.getFileView( + let byteBuffer = try await storage.getFilePreview( bucketId: "[BUCKET_ID]", fileId: "[FILE_ID]" ) @@ -695,7 +692,7 @@ class MainActivity : AppCompatActivity() { val storage = Storage(client) GlobalScope.launch { - val result = storage.getFileView( + val result = storage.getFilePreview( bucketId = "[BUCKET_ID]", fileId = "[FILE_ID]" ) @@ -704,4 +701,5 @@ class MainActivity : AppCompatActivity() { } } ``` +```dart {% /multicode %} \ No newline at end of file