An image style in Object Storage Service (OSS) groups a set of image processing operations under a single name. Instead of repeating the full parameter string each time, apply the style to any image in a bucket to apply operations such as scaling, cropping, rotating, and adding watermarks in one call.
This topic shows how to create, query, and delete image styles using OSS SDK for Python (V1).
Prerequisites
Before you begin, ensure that you have:
An OSS bucket
Access credentials configured as environment variables (
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRET). See Configure access credentials using OSS SDK for Python 1.0The RAM permissions required for the operations you want to perform (see Permissions below)
Permissions
An Alibaba Cloud account has the required permissions by default. To perform image style operations as a Resource Access Management (RAM) user or with Security Token Service (STS), grant the following permissions:
| Operation | Required permission |
|---|---|
| Create an image style | oss:PutStyle |
| Query a specific image style | oss:GetStyle |
| List all image styles | oss:ListStyle |
| Delete an image style | oss:DeleteStyle |
For details, see Grant custom access policies to a RAM user.
Operations
| Operation | SDK method | REST API |
|---|---|---|
| Create an image style | bucket.put_bucket_style(style, content) | PutStyle |
| Get a specific image style | bucket.get_bucket_style(style) | GetStyle |
| List all image styles | bucket.list_bucket_style() | ListStyle |
| Delete an image style | bucket.delete_bucket_style(style) | DeleteStyle |
Client initialization
All examples use the same client initialization pattern. Replace examplebucket and the endpoint with your own values.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Load access credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running any example.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Set the endpoint for the region where your bucket is located.
# Example: China (Hangzhou) region uses https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Set the region that matches the endpoint. Required for V4 signatures.
region = "cn-hangzhou"
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)The examples use the public endpoint for China (Hangzhou). To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. See Regions and endpoints.
To create an OSSClient instance using a custom domain name or STS, see Initialization.
Create an image style
A bucket supports up to 50 image styles.
Method: bucket.put_bucket_style(style, content)
| Parameter | Type | Description |
|---|---|---|
style | str | The name of the image style. |
content | str | The image processing operations to apply, in OSS image processing parameter format. |
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Name of the image style to create.
style = 'imagestyle'
# Define the operations the style applies. This example resizes images to a width of 200 pixels.
content = 'image/resize,w_200'
result = bucket.put_bucket_style(style, content)
print('The style is created. The status code is: ' + str(result.status))After creating a style, apply it to images in the bucket. See Use image styles to process images.
For the full list of supported image processing parameters, see Image processing.
Get an image style
Method: bucket.get_bucket_style(style)
| Parameter | Type | Description |
|---|---|---|
style | str | The name of the image style to retrieve. |
Response fields:
| Field | Description |
|---|---|
result.name | The name of the image style. |
result.content | The image processing operations defined in the style. |
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
style = 'imagestyle'
result = bucket.get_bucket_style(style)
print('name: ' + result.name)
print('content: ' + result.content)List all image styles
Method: bucket.list_bucket_style()
Returns all image styles in the bucket. Each item in result.styles has name and content fields.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
result = bucket.list_bucket_style()
for sty in result.styles:
print('name: ' + sty.name)
print('content: ' + sty.content)Delete an image style
Method: bucket.delete_bucket_style(style)
| Parameter | Type | Description |
|---|---|---|
style | str | The name of the image style to delete. |
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
style = 'imagestyle'
result = bucket.delete_bucket_style(style)
print('Style deleted successfully. Return status: ' + str(result.status))