@@ -31,7 +31,7 @@ const client = new ImageKit({
3131});
3232
3333const response = await client .files .upload ({
34- file: ' https://www.example.com/public-url.jpg ' ,
34+ file: fs . createReadStream ( ' path/to/file ' ) ,
3535 fileName: ' file-name.jpg' ,
3636});
3737
@@ -52,7 +52,7 @@ const client = new ImageKit({
5252});
5353
5454const params: ImageKit .FileUploadParams = {
55- file: ' https://www.example.com/public-url.jpg ' ,
55+ file: fs . createReadStream ( ' path/to/file ' ) ,
5656 fileName: ' file-name.jpg' ,
5757};
5858const response: ImageKit .FileUploadResponse = await client .files .upload (params );
@@ -76,23 +76,17 @@ import ImageKit, { toFile } from '@imagekit/nodejs';
7676const client = new ImageKit ();
7777
7878// If you have access to Node `fs` we recommend using `fs.createReadStream()`:
79- await client .beta . v2 . files .upload ({ file: fs .createReadStream (' /path/to/file' ), fileName: ' fileName' });
79+ await client .files .upload ({ file: fs .createReadStream (' /path/to/file' ), fileName: ' fileName' });
8080
8181// Or if you have the web `File` API you can pass a `File` instance:
82- await client .beta . v2 . files .upload ({ file: new File ([' my bytes' ], ' file' ), fileName: ' fileName' });
82+ await client .files .upload ({ file: new File ([' my bytes' ], ' file' ), fileName: ' fileName' });
8383
8484// You can also pass a `fetch` `Response`:
85- await client .beta . v2 . files .upload ({ file: await fetch (' https://somesite/file' ), fileName: ' fileName' });
85+ await client .files .upload ({ file: await fetch (' https://somesite/file' ), fileName: ' fileName' });
8686
8787// Finally, if none of the above are convenient, you can use our `toFile` helper:
88- await client .beta .v2 .files .upload ({
89- file: await toFile (Buffer .from (' my bytes' ), ' file' ),
90- fileName: ' fileName' ,
91- });
92- await client .beta .v2 .files .upload ({
93- file: await toFile (new Uint8Array ([0 , 1 , 2 ]), ' file' ),
94- fileName: ' fileName' ,
95- });
88+ await client .files .upload ({ file: await toFile (Buffer .from (' my bytes' ), ' file' ), fileName: ' fileName' });
89+ await client .files .upload ({ file: await toFile (new Uint8Array ([0 , 1 , 2 ]), ' file' ), fileName: ' fileName' });
9690```
9791
9892## Handling errors
@@ -104,7 +98,7 @@ a subclass of `APIError` will be thrown:
10498<!-- prettier-ignore -->
10599``` ts
106100const response = await client .files
107- .upload ({ file: ' https://www.example.com/public-url.jpg ' , fileName: ' file-name.jpg' })
101+ .upload ({ file: fs . createReadStream ( ' path/to/file ' ) , fileName: ' file-name.jpg' })
108102 .catch (async (err ) => {
109103 if (err instanceof ImageKit .APIError ) {
110104 console .log (err .status ); // 400
@@ -145,7 +139,7 @@ const client = new ImageKit({
145139});
146140
147141// Or, configure per-request:
148- await client .files .upload ({ file: ' https://www.example.com/public-url.jpg ' , fileName: ' file-name.jpg' }, {
142+ await client .files .upload ({ file: fs . createReadStream ( ' path/to/file ' ) , fileName: ' file-name.jpg' }, {
149143 maxRetries: 5 ,
150144});
151145```
@@ -162,7 +156,7 @@ const client = new ImageKit({
162156});
163157
164158// Override per-request:
165- await client .files .upload ({ file: ' https://www.example.com/public-url.jpg ' , fileName: ' file-name.jpg' }, {
159+ await client .files .upload ({ file: fs . createReadStream ( ' path/to/file ' ) , fileName: ' file-name.jpg' }, {
166160 timeout: 5 * 1000 ,
167161});
168162```
@@ -186,13 +180,13 @@ Unlike `.asResponse()` this method consumes the body, returning once it is parse
186180const client = new ImageKit ();
187181
188182const response = await client .files
189- .upload ({ file: ' https://www.example.com/public-url.jpg ' , fileName: ' file-name.jpg' })
183+ .upload ({ file: fs . createReadStream ( ' path/to/file ' ) , fileName: ' file-name.jpg' })
190184 .asResponse ();
191185console .log (response .headers .get (' X-My-Header' ));
192186console .log (response .statusText ); // access the underlying Response object
193187
194188const { data : response, response : raw } = await client .files
195- .upload ({ file: ' https://www.example.com/public-url.jpg ' , fileName: ' file-name.jpg' })
189+ .upload ({ file: fs . createReadStream ( ' path/to/file ' ) , fileName: ' file-name.jpg' })
196190 .withResponse ();
197191console .log (raw .headers .get (' X-My-Header' ));
198192console .log (response .videoCodec );
0 commit comments