更新时间:2024-09-09 GMT+08:00
多版本对象权限
开发过程中,您有任何问题可以在github上提交issue,或者在华为云对象存储服务论坛中发帖求助。
设置多版本对象访问权限
您可以通过ObsClient.setObjectAcl接口指定VersionId参数设置多版本对象的访问权限,代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
// 引入obs库 // 使用npm安装 const ObsClient = require("esdk-obs-nodejs"); // 使用源码安装 // var ObsClient = require('./lib/obs'); // 创建ObsClient实例 const obsClient = new ObsClient({ // 推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险 // 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, // 【可选】如果使用临时AK/SK和SecurityToken访问OBS,同样建议您尽量避免使用硬编码,以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入 // security_token: process.env.SECURITY_TOKEN, // endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写 server: "https://obs.cn-north-4.myhuaweicloud.com", }); async function setObjectAcl() { try { const params = { // 指定存储桶名称 Bucket: "examplebucket", // 指定对象,此处以 example/objectname 为例 Key: "example/objectname", // 指定版本对象的版本号 VersionId: 'G001117FCE89978B0000401205D5DC9A' // 设置对象所有者 Owner: { 'ID': 'ownerid' }, Grants: [ // 为指定用户设置完全控制权限,此处用户ID为0a03f5833900d3730f13c00f49d5exxx { Grantee: { Type: 'CanonicalUser', ID: 'userid' }, Permission: obsClient.enums.PermissionFullControl }, // 为所有用户设置读权限 { Grantee: { Type: 'Group', URI: obsClient.enums.GroupAllUsers }, Permission: obsClient.enums.PermissionRead }, ] }; // 设置版本对象ACL const result = await obsClient.setObjectAcl(params); if (result.CommonMsg.Status <= 300) { console.log("Set Object(%s)'s acl successful with Bucket(%s)!", params.Key, params.Bucket); console.log("RequestId: %s", result.CommonMsg.RequestId); return; }; console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response."); console.log("Status: %d", result.CommonMsg.Status); console.log("Code: %s", result.CommonMsg.Code); console.log("Message: %s", result.CommonMsg.Message); console.log("RequestId: %s", result.CommonMsg.RequestId); } catch (error) { console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network."); console.log(error); }; }; setObjectAcl(); |
- 使用Owner参数指定对象的所有者信息;使用Grants参数指定被授权的用户信息。
- ACL中需要填写的所有者(Owner)或者被授权用户(Grantee)的ID,是指用户的账户ID,可通过OBS控制台“我的凭证”页面查看。
- 当前OBS对象支持的可被授权的用户组为:
- 所有用户:ObsClient.enums.GroupAllUsers
获取多版本对象访问权限
您可以通过ObsClient.getObjectAcl接口指定VersionId参数获取多版本对象的访问权限,代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
// 引入obs库 // 使用npm安装 const ObsClient = require("esdk-obs-nodejs"); // 使用源码安装 // var ObsClient = require('./lib/obs'); // 创建ObsClient实例 const obsClient = new ObsClient({ // 推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险 // 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, // 【可选】如果使用临时AK/SK和SecurityToken访问OBS,同样建议您尽量避免使用硬编码,以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入 // security_token: process.env.SECURITY_TOKEN, // endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写 server: "https://obs.cn-north-4.myhuaweicloud.com", }); async function getObjectAcl() { try { const params = { // 指定存储桶名称 Bucket: "examplebucket", // 指定下载对象,此处以 example/objectname 为例 Key: 'example/objectname', // 指定版本对象的版本号 VersionId: 'G001117FCE89978B0000401205D5DC9A' }; // 获取版本对象ACL const result = await obsClient.getObjectAcl(params); if (result.CommonMsg.Status <= 300) { console.log("Get object(%s)'s acl successful with bucket(%s)!", params.Key, params.Bucket); console.log("RequestId: %s", result.CommonMsg.RequestId); console.log('Owner[ID]: %s', result.InterfaceResult.Owner.ID); console.log('Owner[Name]: %s', result.InterfaceResult.Owner.Name); for (let i = 0; i < result.InterfaceResult.Grants.length; i++) { const grant = result.InterfaceResult.Grants[i]; fmt.Printf("Grant[%d]-Type:%s, ID:%s, URI:%s, Permission:%s\n", index, grant.Grantee.Type, grant.Grantee.ID, grant.Grantee.URI, grant.Permission); }; return; }; console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response."); console.log("Status: %d", result.CommonMsg.Status); console.log("Code: %s", result.CommonMsg.Code); console.log("Message: %s", result.CommonMsg.Message); console.log("RequestId: %s", result.CommonMsg.RequestId); } catch (error) { console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network."); console.log(error); }; }; getObjectAcl(); |
父主题: 多版本控制