From bd5c86ca75c7a4d4919bad7c106922b9d5e06887 Mon Sep 17 00:00:00 2001 From: lurunze1226 Date: Thu, 16 Feb 2023 20:37:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=9F=A5=E7=9C=8B?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E8=BD=AF=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bos_client.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- src/headers.js | 2 ++ test/config.js | 8 ++++---- test/index.js | 8 ++++++++ 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 test/index.js diff --git a/src/bos_client.js b/src/bos_client.js index 75da5fa6..a9cc7a28 100644 --- a/src/bos_client.js +++ b/src/bos_client.js @@ -1013,6 +1013,47 @@ BosClient.prototype.postObject = function (bucketName, key, data, options) { }); }; +/** + * 获取软连接,需要对软连接有读取权限,接口响应头的x-bce-symlink-target指向目标文件 + */ +BosClient.prototype.getSymlink = function (bucketName, objectName, options) { + options = options || {}; + + return this.sendRequest('GET', { + bucketName, + key: objectName, + params: {symlink: ''}, + config: options.config + }); +} + +/** + * 为BOS的相同bucket下已有的目的object创建软链接 + * @param {string} bucketName 桶名称 + * @param {string} objectName 软连接文件名称 + * @param {string} target 目标对象名称 + * @param {boolean} overwrite 是否覆盖同名Object,默认允许覆盖 + */ +BosClient.prototype.putSymlink = function (bucketName, objectName, target, overwrite, options) { + options = options || {}; + var headers = {}; + + if (!target) { + throw new TypeError('target object should not be empty.'); + } + + headers[H.X_BCE_SYMLINK_TARGET] = target; + headers[H.X_BCE_FORBID_OVERWRITE] = overwrite === true; + + return this.sendRequest('PUT', { + bucketName, + key: objectName, + params: {symlink: ''}, + headers: headers, + config: options.config + }); +} + // --- E N D --- BosClient.prototype.sendRequest = function (httpMethod, varArgs) { @@ -1128,7 +1169,9 @@ BosClient.prototype._prepareObjectHeaders = function (options) { H.X_BCE_STORAGE_CLASS, H.X_BCE_SERVER_SIDE_ENCRYPTION, H.X_BCE_RESTORE_DAYS, - H.X_BCE_RESTORE_TIER + H.X_BCE_RESTORE_TIER, + H.X_BCE_SYMLINK_TARGET, + H.X_BCE_FORBID_OVERWRITE ]; var metaSize = 0; var headers = u.pick(options, function (value, key) { diff --git a/src/headers.js b/src/headers.js index b7bb599b..77dc35df 100644 --- a/src/headers.js +++ b/src/headers.js @@ -42,6 +42,8 @@ exports.X_BCE_STORAGE_CLASS = 'x-bce-storage-class'; exports.X_BCE_SERVER_SIDE_ENCRYPTION = 'x-bce-server-side-encryption'; exports.X_BCE_RESTORE_DAYS = 'x-bce-restore-days'; exports.X_BCE_RESTORE_TIER = 'x-bce-restore-tier'; +exports.X_BCE_SYMLINK_TARGET = 'x-bce-symlink-target'; +exports.X_BCE_FORBID_OVERWRITE = 'x-bce-forbid-overwrite'; exports.X_HTTP_HEADERS = 'http_headers'; exports.X_BODY = 'body'; diff --git a/test/config.js b/test/config.js index 72da3f92..044caf65 100644 --- a/test/config.js +++ b/test/config.js @@ -19,10 +19,10 @@ module.exports = { 'ak': process.env.BOS_AK, 'sk': process.env.BOS_SK, }, - 'account': { - 'id': process.env.ONLINE_USER_ID || '04e0d2c9e8ef478c951b97714c092f77', - 'displayName': process.env.ONLINE_USER_NAME || 'PASSPORT:105016607' - } + // 'account': { + // 'id': process.env.ONLINE_USER_ID || '04e0d2c9e8ef478c951b97714c092f77', + // 'displayName': process.env.ONLINE_USER_NAME || 'PASSPORT:105016607' + // } }, 'bcc': { 'endpoint': 'http://bcc.bce-api.baidu.com', diff --git a/test/index.js b/test/index.js new file mode 100644 index 00000000..9ed0bcbe --- /dev/null +++ b/test/index.js @@ -0,0 +1,8 @@ + +const BosClient = require('../').BosClient; +const config = require('./config'); + +console.log(config.bos); +const client = new BosClient(config.bos); + +(async function () {})()