UNPKG

baidubce-cli

Version:

baidu cloud engine command line tools

150 lines (117 loc) 4.52 kB
/** * Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ var fs = require('fs'); var u = require('underscore'); var glob = require('glob'); var async = require('async'); var Q = require('q'); var debug = require('debug')('cmd_put'); exports.uploadDirectory = function (client, bucket_name, key, directory) { debug('bucket_name = %j, key = %j, directory = %j', bucket_name, key, directory); var stat = fs.lstatSync(directory); if (!stat.isDirectory()) { throw new Error('Invalid file type'); } var failedItems = []; function iterator(item, callback) { var objectKey = key ? (key + item) : item; client.putObjectFromFile(bucket_name, objectKey, item) .then(function () { console.log('[DONE]:%s => bos://%s/%s', item, bucket_name, objectKey); }) .catch(function (error) { console.error(error); failedItems.push(item); }) .fin(callback); } function done() { if (failedItems.length) { console.log('\n -- FAILED ITEMS --'); console.log(failedItems.join('\n')); } else { console.log('All goes well.'); } } glob('**/*', {nodir: true, cwd: directory}, function (er, files) { async.eachLimit(files, 2, iterator, done); }); }; exports.putRegularFile = function (client, bucket_name, key, filename) { debug('putRegularFile bucket_name = %j, key = %j, filename = %j', bucket_name, key, filename); var stat = fs.lstatSync(filename); if (!stat.isFile()) { throw new Error('Invalid file type'); } client.putObjectFromFile(bucket_name, key, filename) .then(function () { console.log(client.generatePresignedUrl(bucket_name, key)); }) .catch(function (error) { console.error(error); }); }; exports.putSuperLargeFile = function (client, bucket_name, key, filename) { var stat = fs.lstatSync(filename); if (!stat.isFile()) { throw new Error('Invalid file type'); } var upload_id = null; client.initiateMultipartUpload(bucket_name, key) .then(function (response) { upload_id = response.body.uploadId; var left_size = fs.statSync(filename).size; var min_part_size = 5 * 1024 * 1024; var part_count = Math.ceil(left_size / min_part_size); var success_count = 0; var offset = 0; var part_number = 1; var defers = []; function displayProgress(response) { success_count += 1; require('./util').displayProgress(Math.floor(success_count * 100 / part_count)); return response; } while (left_size > 0) { var part_size = Math.min(left_size, min_part_size); defers.push(client.uploadPartFromFile(bucket_name, key, upload_id, part_number, part_size, filename, offset).then(displayProgress)); left_size -= part_size; offset += part_size; part_number += 1; } return Q.all(defers); }) .then(function (all_response) { console.log(); var part_list = []; u.each(all_response, function (response, index) { part_list.push({ partNumber: index + 1, eTag: response.http_headers.etag }); }); return client.completeMultipartUpload(bucket_name, key, upload_id, part_list); }) .then(function () { console.log(client.generatePresignedUrl(bucket_name, key)); }) .catch(function (error) { console.error(error); }); }; /* vim: set ts=4 sw=4 sts=4 tw=120: */