This topic describes how to configure, query, and delete the policies of a bucket.

Background information

Bucket policies provide resource-based authorization for users. Bucket policies apply to the following scenarios:

  • Authorize RAM users of other accounts to access your OSS resources.

    You can authorize RAM users of other accounts to access your OSS resources.

  • Authorize anonymous users to access your OSS resources using specific IP addresses or IP ranges.

    In some cases, you must authorize anonymous users to access OSS resources using specific IP addresses or IP ranges. For example, confidential documents of an enterprise are only allowed to be accessed within the enterprise but not in other regions. Previously, configuring RAM policies for every user was a tedious and complex task because of the potential for a large number of internal users. To resolve this issue, you can configure access policies with IP restrictions based on bucket policies to authorize a large number of users easily and efficiently.

For more information about bucket policy configurations and use cases, see Use bucket policies to authorize other users to access OSS resources. For more information about bucket policy syntax, see Policy structure and syntax.

Configure bucket policies

The following code provides an example on how to configure a bucket policy:

package main

import (
    "fmt"
    "os"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Configure a bucket policy.
    policyConfig := `
    {
        "Statement": [
            {
                "Action": [
                    "oss:GetObject",
                    "oss:ListObjects"
                ],
                "Effect" : "Allow",
                "Resource" : ["acs:oss:*:*:*/user1/*"]
            }
        ],
        "Version": "1"
    }`

    // Configure the policy for the bucket.
    err = client.SetBucketPolicy("<yourBucketName>", policyConfig)
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Println("SetBucketPolicy success")
}

For more information about how to configure bucket policies, see PutBucketPolicy.

Query bucket policies

The following code provides an example on how to query the policies configured for a bucket:

package main

import (
    "fmt"
    "os"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Obtain the bucket policy.
    strPolicy, err := client.GetBucketPolicy("<yourBucketName>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Println("Bucket policy:", strPolicy)
}

For more information about how to query bucket policies, see GetBucketPolicy.

Delete bucket policies

The following code provides an example on how to delete the policies configured for a bucket:

package main

import (
    "fmt"
    "os"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Delete the bucket policy.
    err = client.DeleteBucketPolicy("<yourBucketName>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    fmt.Println("DeleteBucketPolicy success")
}

For more information about how to delete bucket policies, see DeleteBucketPolicy.