This tutorial uses a business scenario to demonstrate how to configure fine-grained access permissions for OSS buckets, folders, and files. You will learn how the flat storage structure and prefix listing mechanism of OSS affect policy configuration and how to grant folder-level read/write, read-only, and deny access permissions to different Resource Access Management (RAM) users.
Storage structure and authorization
OSS storage model
OSS uses a flat storage model, which stores objects as key-value pairs directly within a bucket. No physical directory hierarchy exists. The folders that you see in the console are simulated using object key prefixes and a separator (/). This lets you group, classify, and manage objects.
For the examplebucket bucket, the folder structure and corresponding Object Keys are displayed in the console as follows:
examplebucket
├── Development/ # Key: Development/ (folder object)
│ ├── Alibaba Cloud.pdf # Key: Development/Alibaba Cloud.pdf
│ ├── ProjectA.docx # Key: Development/ProjectA.docx
│ └── ProjectB.docx # Key: Development/ProjectB.docx
├── Marketing/ # Key: Marketing/ (folder object)
│ ├── data2020.xlsx # Key: Marketing/data2020.xlsx
│ └── data2021.xlsx # Key: Marketing/data2021.xlsx
├── Private/ # Key: Private/ (folder object)
│ └── 2017/ # Key: Private/2017/ (folder object)
│ ├── images.zip # Key: Private/2017/images.zip
│ └── promote.pptx # Key: Private/2017/promote.pptx
└── oss-dg.pdf # Key: oss-dg.pdfThe key of a folder object ends with /. OSS uses this as a flag to identify the object as a folder. However, all objects, including folder objects, are stored in a flat structure.
Because OSS does not have a real directory hierarchy, when you grant access to a folder, you are actually granting access to all objects that have a specific prefix. For example, granting access to the Development/ folder is equivalent to granting access to all objects whose keys start with Development/.
Differences in requests for buckets, folders, and files
Operations on different targets, such as buckets, folders, and files, correspond to different API requests and Resource configurations. Understanding these differences is key to correctly configuring access policies.
ListObjects request mechanism
Displaying the folder structure in the console's file list relies on two core parameters of the GetBucket (ListObjects) API operation:
Parameter | Function | Example value |
| Limits the returned objects to those whose keys start with this prefix. |
|
| The character used to group objects. This is usually |
|
When a user clicks the Development/ folder in the console, the console sends the following request to OSS:
GET /?prefix=Development/&delimiter=/ HTTP/1.1
Host: examplebucket.oss-cn-hangzhou.aliyuncs.comOSS returns all objects that start with Development/. Because delimiter=/ is specified, OSS returns subfolders, such as Development/SubFolder/, as CommonPrefixes instead of listing all the files within them.
Resource configurations for different operations
Operation target | Triggered API | Resource configuration | Key points for policy configuration |
List the root directory of a bucket | ListObjects, with an empty prefix and |
| The Resource element points to the bucket itself, not a specific path. |
Enter a folder | ListObjects, with prefix set to |
| The Resource element points to the bucket itself, not a specific path. The listable range is restricted by the |
Read or write file content |
| The Resource element can specify a path and supports the wildcard character |
Differences between console and API/SDK access
Accessing OSS through the console requires more permissions than accessing it directly using an API or SDK. This is because a user needs to navigate from the bucket list to the target folder.
Access method | Required permissions |
API/SDK | Only permissions for the target resource operation are required, such as |
Console | In addition to permissions on the target resource, auxiliary permissions such as |
Scenario
Assume that the access control list (ACL) for all objects in the examplebucket bucket is set to private by default. The bucket structure is as follows:
examplebucket
├── Development/ # Development department folder
│ ├── Alibaba Cloud.pdf
│ ├── ProjectA.docx
│ └── ProjectB.docx
├── Marketing/ # Marketing department folder
│ ├── data2020.xlsx
│ └── data2021.xlsx
├── Private/ # Confidential folder
│ └── 2017/
│ ├── images.zip
│ └── promote.pptx
└── oss-dg.pdfThe access control objectives are as follows:
RAM user/User group | Authorization target | Permission type |
RAM user Anne | The | Read and write permissions |
RAM user Leo | The | Read-only permissions |
All members of a specified user group | The | Access denied |
Step 1: Create a bucket and upload files
Go to the Bucket list page and click Create Bucket. Name the bucket
examplebucket.In the bucket, click Create Folder to create the
Development,Marketing, andPrivatefolders. Then, create a2017subfolder in thePrivatefolder.Click Upload File and upload files to the following paths:
Root directory:
oss-dg.pdfDevelopment/folder:Alibaba Cloud.pdf,ProjectA.docx,ProjectB.docxMarketing/folder:data2020.xlsx,data2021.xlsxPrivate/2017/folder:images.zip,promote.pptx
Step 2: Create RAM users Anne and Leo
Go to the User List page and click Create User. Create the users Anne and Leo.
Step 3: Grant Anne read and write permissions on the Development folder
Policy design approach
To grant read and write access to the Development/ folder, you need to grant two types of permissions:
List permission: Allows a user to list the objects in the
Development/folder (oss:ListObjects). AConditionrestricts the listing to objects that start with theDevelopmentprefix.Read and write permissions: Grants permission to read (
oss:GetObject) and upload (oss:PutObject) files in theDevelopment/folder. The Resource element points toexamplebucket/Development/*.
Create and grant the policy
Go to the RAM Policies page and click Create Policy.
Select the Script Editor tab and enter the following policy content:
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": "oss:ListObjects", "Resource": "acs:oss:*:*:examplebucket", "Condition": { "StringLike": { "oss:Prefix": [ "Development", "Development/*" ] } } }, { "Effect": "Allow", "Action": [ "oss:GetObject", "oss:PutObject", "oss:GetObjectAcl" ], "Resource": "acs:oss:*:*:examplebucket/Development/*" } ] }Policy analysis:
First Statement: Grants the list permission on the bucket. The
Conditionwithoss:Prefixrestricts the listing to content within theDevelopmentfolder and its subfolders.Second Statement: Grants read and write permissions. The Resource points to all objects in the
Development/*path.
Click OK. Enter a Policy Name, such as
AllowAnneAccessDevelopment, and click OK to create the policy.Go to the User List page. Find the RAM user Anne, click Add Permissions, and select the policy that you just created.
Step 4: Grant Leo read-only permissions on the Marketing folder
Follow the procedure in Step 3 to create and grant a read-only policy for the RAM user Leo. The only difference from the read/write policy is that the Action element includes only read-related operations. The policy content is as follows:
{
"Version": "1",
"Statement": [
{
"Effect": "Allow",
"Action": "oss:ListObjects",
"Resource": "acs:oss:*:*:examplebucket",
"Condition": {
"StringLike": {
"oss:Prefix": [
"Marketing",
"Marketing/*"
]
}
}
},
{
"Effect": "Allow",
"Action": [
"oss:GetObject",
"oss:GetObjectAcl"
],
"Resource": "acs:oss:*:*:examplebucket/Marketing/*"
}
]
}The oss:PutObject action is removed. Leo can only read files but cannot upload or modify them.
Step 5: Deny a specified user group access to the Private folder
You can manage permissions centrally using a user group. Add the RAM users who should be denied access to the Private/ folder to the same user group. Then, attach a deny policy to that group.
This method applies only to RAM users who are members of the user group. To deny access for all RAM users in your account, including new ones, you must configure a deny policy on the bucket using a bucket policy.
Create a user group and add members
Go to the RAM User Groups page and click Create User Group. For example, name it
DenyPrivateAccessGroup.Click Add Members and add the RAM users who should be denied access to the
Private/folder to the user group.
Create and grant the deny policy
Go to the RAM Policies page and click Create Policy.
Select the Script Editor tab and enter the following policy content:
{ "Version": "1", "Statement": [ { "Effect": "Deny", "Action": "oss:*", "Resource": "acs:oss:*:*:examplebucket/Private/*" }, { "Effect": "Deny", "Action": "oss:ListObjects", "Resource": "acs:oss:*:*:examplebucket", "Condition": { "StringLike": { "oss:Prefix": [ "Private/", "Private/*" ] } } } ] }Policy analysis:
First Statement: Denies all operations on any object under the
Private/path.Second Statement: Denies listing the contents of the
Private/folder to prevent users from viewing the file list.
Click OK. Enter a Policy Name, such as
DenyAccessPrivateFolder, and click OK to create the policy.Go to the RAM User Groups page. For the user group, click Add Permissions and select the policy that you just created.
When a RAM user in the group tries to access the Private/ folder, OSS returns a permission denied error, regardless of whether the user is trying to list or download files.