All Products
Search
Document Center

Object Storage Service:Image styles (Python SDK V1)

Last Updated:Mar 19, 2026

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:

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:

OperationRequired permission
Create an image styleoss:PutStyle
Query a specific image styleoss:GetStyle
List all image stylesoss:ListStyle
Delete an image styleoss:DeleteStyle

For details, see Grant custom access policies to a RAM user.

Operations

OperationSDK methodREST API
Create an image stylebucket.put_bucket_style(style, content)PutStyle
Get a specific image stylebucket.get_bucket_style(style)GetStyle
List all image stylesbucket.list_bucket_style()ListStyle
Delete an image stylebucket.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)

ParameterTypeDescription
stylestrThe name of the image style.
contentstrThe 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)

ParameterTypeDescription
stylestrThe name of the image style to retrieve.

Response fields:

FieldDescription
result.nameThe name of the image style.
result.contentThe 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)

ParameterTypeDescription
stylestrThe 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))

What's next