3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

BoxにNode.jsからファイルアップロードメモ

Last updated at Posted at 2022-04-12

BoxのAPIを使ってファイルアップロードを試してみます。

シンプルなサンプル

こちらのSDKを使って試してみましたが、割とシンプルに動きました。

ドキュメントを見つつasync/awaitに書き換えてます。

https://github.com/box/box-node-sdk/blob/main/docs/files.md#upload-a-file

'use strict';

const BoxSDK = require('box-node-sdk');
const fs = require('fs');

// Initialize the SDK with your app credentials
const sdk = new BoxSDK({
  clientID: 'xxxxxxxxxxx',
  clientSecret: 'xxxxxxxxxxxx'
});
const client = sdk.getBasicClient('xxxxxxxxxxxxxx');


const main = async () => {
    const fileName = `test.png`;
    const uploadBoxFolderId = 'xxxxxxxxxxxxxx';
    
    try {
        const stream = fs.createReadStream(`./${fileName}`);
        const file = await client.files.uploadFile(uploadBoxFolderId, fileName, stream)
        console.log(file);
    } catch (err) {
        console.log(err);
    }
}   

main();

分割アップロード(Chunked Upload)/ 大きいファイル

大きいファイルを扱う場合は分割アップロードをするのが良いらしいです。

https://github.com/box/box-node-sdk/blob/main/docs/files.md#chunked-upload

getChunkedUploader()という関数があるので使ってみます。

//省略

const main = async () => {
    const fileName = `my.pdf`;
    const uploadBoxFolderId = '160667378861';
    try {
        const data = fs.readFileSync(`./${fileName}`);
        const stream = fs.createReadStream(`./${fileName}`);
        client.files.getChunkedUploader(uploadBoxFolderId, data.length, fileName, stream)
        .then(uploader => uploader.start())
        .then(file => {
            console.log(file);
        });
    } catch (err) {
        console.log(err);
    }
}   

main();

うまくいきました。ちなみに、この時使ったpdfは66MBでした。

小さいファイルを上げようとすると

スクリーンショット 2022-04-12 17.02.21.png

Unhandled rejection Error: Unexpected API Response [400 Bad Request | 39e1f076a25cfd2867df145166f395cc] file_size_too_small - File size 225156 less than minimum allowed for this API: 20000000

エラーを見る限りだとおそらく 20MB未満は分割アップロードでは使えない模様です。

逆に20MB以上は分割アップロードをするのが良さそうですね。

tipsなど

フォルダーIDについて

  • ブラウザで開いた時のURLの/folder/xxxxxxxxxxxのxxxxxxxxx部分
    • この場合は160667378861がフォルダーID

  • ルートディレクトリのIDが0で固定っぽい

参考: https://qiita.com/yama9112/items/e548083a38365d6ec951

デフォルトで60分らしい、開発者トークン

デフォルトで60分らしい。
よく分からず試していて、急に動かなくなって焦りました。

https://ja.developer.box.com/guides/authentication/tokens/developer-tokens/

アクションスコープのチェック

試した環境だとデフォルトで書き込み権限にチェックが入ってなかったのでこちらを忘れずに設定しておく。

スクリーンショット 2022-04-12 16.52.37.png

そのまま上書きは出来ない

Google Driveなどの仕様とは違うみたいで、同じファイル名でアップロードしようとすると怒られました。

スクリーンショット 2022-04-12 17.10.33.png
Unhandled rejection Error: Unexpected API Response [409 Conflict | 7zmyo4h1706s8fpi] item_name_in_use - Item with the same name already exists

まとめ

慣れると比較的簡単に使えましたが、設定周りの癖がありますね。
慣れるとGoogle系APIより楽かもしれません。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?