管理存储空间

Bucket 是 MSS 上的存储空间,也是权限、计费等功能的管理单元。

创建Bucket

创建 Bucket。默认情况下创建的 Bucket 是 private 的,只有 Bucket 的创建者可以访问。 可以 Put 请求中的“x-amz-acl”头来设置 Bucket 访问权限。目前 Bucket 有二种访问权限:public-read 和 private。

var MSS = require('mos-mss');
var client = new MSS({
    accessKeyId: '<accessKeyId>',
    accessKeySecret: '<accessKeySecret>'
});

var result = client.createBucket('Bucket Name');
result.then(function (res) {
    console.log(res);
});

判断Bucket是否存在

var MSS = require('mos-mss');
var client = new MSS({
    accessKeyId: '<accessKeyId>',
    accessKeySecret: '<accessKeySecret>'
});

var result = client.getBucket('Bucket Name');
result.then(function (res) {
    console.log(res);
    //res.code === 200 存在
});

列举Bucket

对于服务地址作 Get 请求可以返回请求者拥有的所有 Bucket,其中“/”表示根目录。

var MSS = require('mos-mss');
var client = new MSS({
    accessKeyId: '<accessKeyId>',
    accessKeySecret: '<accessKeySecret>'
});

var result = client.listBucket();
result.then(function (res) {
    console.log(res);
});

删除Bucket

deleteBucket 用于删除某个 Bucket。

var MSS = require('mos-mss');

var client = new MSS({
    accessKeyId: '<accessKeyId>',
    accessKeySecret: '<accessKeySecret>'
});

var result = client.deleteBucket('Bucket Name');
result.then(function (res) {
    console.log(res);
});

注意: MSS 禁止删除非空 bucket,对于非空 bucket,请先参考管理对象一节删除该 bucket 下的所有对象。

设置权限

MSS 中的 bucket 只有两种权限:“private”和“public-read",详情参考 MSS 基本概念中的访问控制一节。

var MSS = require('mos-mss');
var client = new MSS({
    accessKeyId: '<accessKeyId>',
    accessKeySecret: '<accessKeySecret>'
});

var result = client.putBucketACL('Bucket Name', 'ACL');
result.then(function (res) {
    console.log(res);
});

查看权限

getBucketACL 用来获取某个 Bucket 的访问权限。

var MSS = require('mos-mss');
var client = new MSS({
    accessKeyId: '<accessKeyId>',
    accessKeySecret: '<accessKeySecret>'
});

var result = client.getBucketACL('Bucket Name');
result.then(function (res) {
    console.log(res);
});

BucketPolicy(策略)管理

获取policy

var MSS = require('../dist/es5');

var { accessKeyId, accessKeySecret } = require('./config/server.js');

var client = new MSS({
    accessKeyId,
    accessKeySecret
});

var result = client.getBucketPolicy('test-bucket');
result.then(function (res) {
    console.log(JSON.stringify(res));
});

设置policy

var MSS = require('../dist/es5');

var { accessKeyId, accessKeySecret } = require('./config/server.js');

var client = new MSS({
    accessKeyId,
    accessKeySecret
});

var result = client.putBucketPolicy('test-bucket', {
    policy: {
        "Version":"2012-10-17",
        "Statement":[
            // refer 不相关部分
            {
                "Sid":"",
                "Effect":"Allow",
                "Principal": {"AWS": "*"},
                "Action":["s3:PutObject", "s3:PutObjectAcl"],
                "Resource":["arn:aws:s3:::examplebucket/*"]
            },
            // refer 相关部分
            {
                "Sid":"",
                "Effect":"Allow",
                "Principal":{"AWS": "*"},
                "Action":["s3:GetObject"],
                "Resource":["arn:aws:s3:::examplebucket/*"],
                "Condition":{
                    "StringLike":{"aws:Referer":["http://www.example.com/*", "http://example.com/*", ""]}
                }
            },
            {
                "Sid":"Allow get requests without referrer",
                "Effect":"Deny",
                "Principal":{"AWS": "*"},
                "Action":["s3:GetObject"],
                "Resource":["arn:aws:s3:::examplebucket/*"],
                "Condition":{
                            "StringNotLike":{"aws:Referer":["http://www.example.com/*", "http://example.com/*", ""]}
                }
            }
        ]
    }
});
result.then(function (res) {
    console.log(JSON.stringify(res));
});