Skip to content

Commit d936538

Browse files
feat: add new wrapper method for upload file as Buffer (#488)
* Update README.md to change API network from TAURUS to MAINNET for consistency in examples * Add `uploadFileFromBuffer` method to wrappers * Update README.md to include instructions for using the `uploadFileFromBuffer` method * Update packages/auto-drive/README.md Co-authored-by: Jeremy Frank <[email protected]> --------- Co-authored-by: Jeremy Frank <[email protected]>
1 parent 706f988 commit d936538

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

packages/auto-drive/README.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,34 @@ To interact with the Auto-Drive API, you'll need to create an API key. Follow th
2626
- Once you're logged in, click on the developers section in the left sidebar menu.
2727
- In the developers section, click on 'Create API Key'
2828
- Read the modal message and click on generate
29+
Add
30+
### How to upload a file from Buffer?
31+
32+
Here is an example of how to use the `uploadFileFromBuffer` method to upload a Buffer with optional encryption and compression:
33+
34+
```typescript
35+
import { createAutoDriveApi } from '@autonomys/auto-drive'
36+
import { NetworkId } from '@autonomys/auto-utils'
37+
38+
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.MAINNET }) // Initialize your API instance with API key
39+
40+
// Create a buffer from your data
41+
const buffer = Buffer.from('Hello, Autonomys!')
42+
const fileName = 'hello.txt'
43+
44+
const options = {
45+
password: 'your-encryption-password', // Optional: specify a password for encryption
46+
compression: true,
47+
// an optional callback useful for large file uploads
48+
onProgress?: (progress: number) => {
49+
console.log(`The upload is ${progress}% completed`)
50+
}
51+
}
52+
53+
const cid = await api.uploadFileFromBuffer(buffer, fileName, options)
54+
55+
console.log(`The file is uploaded and its cid is ${cid}`)
56+
```
2957

3058
### How to upload a file from filepath? (Not available in browser)
3159

@@ -35,7 +63,7 @@ Here is an example of how to use the `fs.uploadFileFromFilepath` method to uploa
3563
import { fs, createAutoDriveApi } from '@autonomys/auto-drive'
3664
import { NetworkId } from '@autonomys/auto-utils'
3765

38-
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key
66+
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.MAINNET }) // Initialize your API instance with API key
3967
const filePath = 'path/to/your/file.txt' // Specify the path to your file
4068
const options = {
4169
password: 'your-encryption-password', // Optional: specify a password for encryption
@@ -57,7 +85,7 @@ console.log(`The file is uploaded and its cid is ${cid}`)
5785
import { createAutoDriveApi } from '@autonomys/auto-drive'
5886
import { NetworkId } from '@autonomys/auto-utils'
5987

60-
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key
88+
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.MAINNET }) // Initialize your API instance with API key
6189

6290
// e.g Get File from object from HTML event
6391
const file: File = e.target.value // Substitute with your file
@@ -92,7 +120,7 @@ You could upload any file that could be represented in that way. For example, up
92120
import { createAutoDriveApi } from '@autonomys/auto-drive'
93121
import { NetworkId } from '@autonomys/auto-utils'
94122

95-
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key
123+
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.MAINNET }) // Initialize your API instance with API key
96124
const buffer = Buffer.from(...);
97125
const genericFile = {
98126
read: async function *() {
@@ -124,7 +152,7 @@ console.log(`The file is uploaded and its cid is ${cid}`)
124152
import { createAutoDriveApi, fs } from '@autonomys/auto-drive'
125153
import { NetworkId } from '@autonomys/auto-utils'
126154

127-
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key
155+
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.MAINNET }) // Initialize your API instance with API key
128156
const folderPath = 'path/to/your/folder' // Specify the path to your folder
129157

130158
const options = {
@@ -151,7 +179,7 @@ Here is an example of how to use the `downloadFile` method to download a file fr
151179
import { createAutoDriveApi } from '@autonomys/auto-drive'
152180
import { NetworkId } from '@autonomys/auto-utils'
153181

154-
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key
182+
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.MAINNET }) // Initialize your API instance with API key
155183

156184
try {
157185
const cid = '..'
@@ -176,7 +204,7 @@ Here are examples of how to use the object moderation methods:
176204
import { createAutoDriveApi } from '@autonomys/auto-drive'
177205
import { NetworkId } from '@autonomys/auto-utils'
178206

179-
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS })
207+
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.MAINNET })
180208

181209
try {
182210
const cid = 'your-object-cid'
@@ -193,7 +221,7 @@ try {
193221
import { createAutoDriveApi } from '@autonomys/auto-drive'
194222
import { NetworkId } from '@autonomys/auto-utils'
195223

196-
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS })
224+
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.MAINNET })
197225

198226
try {
199227
const cid = 'your-object-cid'
@@ -210,7 +238,7 @@ try {
210238
import { createAutoDriveApi } from '@autonomys/auto-drive'
211239
import { NetworkId } from '@autonomys/auto-utils'
212240

213-
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS })
241+
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.MAINNET })
214242

215243
try {
216244
const cid = 'your-object-cid'
@@ -227,7 +255,7 @@ try {
227255
import { createAutoDriveApi } from '@autonomys/auto-drive'
228256
import { NetworkId } from '@autonomys/auto-utils'
229257

230-
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS })
258+
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.MAINNET })
231259

232260
try {
233261
const toBeReviewed = await api.getToBeReviewedList(50, 0)
@@ -248,7 +276,7 @@ Here is an example of how to use the `publishObject` method to publish an object
248276
import { createAutoDriveApi } from '@autonomys/auto-drive'
249277
import { NetworkId } from '@autonomys/auto-utils'
250278

251-
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key
279+
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.MAINNET }) // Initialize your API instance with API key
252280

253281
try {
254282
const cid = 'your-file-cid'
@@ -269,7 +297,7 @@ Here is an example of how to use the `getMyFiles` method to retrieve the root di
269297
import { createAutoDriveApi } from '@autonomys/auto-drive'
270298
import { NetworkId } from '@autonomys/auto-utils'
271299

272-
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key
300+
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.MAINNET }) // Initialize your API instance with API key
273301

274302
try {
275303
for (let i = 0; i < 10; i++) {

packages/auto-drive/src/api/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ export interface AutoDriveApi extends AutoDriveApiHandler {
6565
options: UploadFileOptions,
6666
uploadChunkSize?: number,
6767
) => Promise<string>
68+
/**
69+
* Uploads a buffer to the server with optional encryption and compression.
70+
*
71+
* @param {AutoDriveApi} api - The API instance used to send requests.
72+
* @param {Buffer} buffer - The buffer to be uploaded.
73+
* @param {string} name - The name of the file to be uploaded.
74+
* @param {UploadFileOptions} options - Options for the upload process.
75+
* @param {number} [uploadChunkSize] - The size of each chunk to upload (optional).
76+
* @returns {Promise<string>} - The CID of the uploaded file.
77+
* @throws {Error} - Throws an error if the upload fails at any stage.
78+
*/
79+
uploadFileFromBuffer: (
80+
buffer: Buffer,
81+
name: string,
82+
options: UploadFileOptions,
83+
uploadChunkSize?: number,
84+
) => Promise<string>
6885
/**
6986
* Uploads an object as a JSON file to the server.
7087
*

packages/auto-drive/src/api/wrappers.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import {
33
asyncByChunk,
44
asyncFromStream,
5+
bufferToAsyncIterable,
56
bufferToIterable,
67
fileToIterable,
78
} from '@autonomys/asynchronous'
@@ -152,6 +153,19 @@ export const createApiInterface = (api: AutoDriveApiHandler): AutoDriveApi => {
152153
return result.cid
153154
}
154155

156+
const uploadFileFromBuffer = async (
157+
buffer: Buffer,
158+
name: string,
159+
options: UploadFileOptions = {},
160+
uploadChunkSize?: number,
161+
): Promise<string> => {
162+
return uploadFile(
163+
{ read: () => bufferToAsyncIterable(buffer), name, size: buffer.length },
164+
options,
165+
uploadChunkSize,
166+
)
167+
}
168+
155169
const uploadObjectAsJSON = async (
156170
object: unknown,
157171
name?: string | undefined,
@@ -351,6 +365,7 @@ export const createApiInterface = (api: AutoDriveApiHandler): AutoDriveApi => {
351365
me,
352366
uploadFileFromInput,
353367
uploadFile,
368+
uploadFileFromBuffer,
354369
uploadObjectAsJSON,
355370
uploadFolderFromInput,
356371
uploadFileWithinFolderUpload,

0 commit comments

Comments
 (0)