UNPKG

baidubce-cli

Version:

baidu cloud engine command line tools

98 lines (73 loc) 3.06 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 Q = require('q'); var debug = require('debug')('cmd_put'); exports.putRegularFile = function (client, bucket_name, key, filename) { debug('putRegularFile bucket_name = %j, key = %j, filename = %j', bucket_name, key, filename); client.putObjectFromFile(bucket_name, key, filename) .then(function () { console.log('%s/%s/%s', client.config.endpoint, bucket_name, key); }) .catch(function (error) { console.error(error); }); }; exports.putSuperLargeFile = function (client, bucket_name, key, filename) { 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('../bos/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: */