In Object Storage Service (OSS), an image style lets you perform a series of image processing operations, such as scaling, cropping, rotating, and adding watermarks. You can create an image style to define these operations and then apply the style to images in a bucket for quick processing and transformation.
Notes
For more information about the image processing parameters that image styles support, see Image processing.
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials using OSS SDK for Python 1.0.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
Permissions
An Alibaba Cloud account has the required permissions for image style operations by default. To perform these operations as a Resource Access Management (RAM) user or using STS, you must have the corresponding permissions:
To create an image style, you must have the
oss:PutStylepermission.To query information about a specific image style in a bucket, you must have the
oss:GetStylepermission.To query all created image styles in a bucket, you must have the
oss:ListStylepermission.To delete a specific image style in a bucket, you must have the
oss:DeleteStylepermission.
For more information, see Grant custom access policies to a RAM user.
Create an image style
You can create up to 50 image styles for a bucket. The following code shows how to create an image style:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region of the endpoint. Example: cn-hangzhou. This parameter is required if you use the V4 signature algorithm.
region = "cn-hangzhou"
# Replace examplebucket with the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Specify the name of the image style, such as imagestyle.
style = 'imagestyle'
# Specify the operation of the image style, such as resizing the image to a width of 200 pixels.
content = 'image/resize,w_200'
# Create an image style.
result = bucket.put_bucket_style(style, content)
print('The style is created. The status code is: ' + str(result.status))After you create an image style, you can use the image style to process images. For more information, see Use image styles to process images.
Query a specified image style
The following code shows how to query information about a specified image style in a bucket.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this code, set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Set endpoint to the endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Set region to the region that corresponds to your endpoint, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace examplebucket with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Specify the name of the image style, such as imagestyle.
style = 'imagestyle'
# Query information about the specified image style in the examplebucket bucket.
result = bucket.get_bucket_style(style)
print('name: ' + result.name)
print('content: ' + result.content)Query all image styles
The following code shows how to query all created image styles in a bucket.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before running this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the endpoint, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace examplebucket with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Query all created image styles in the examplebucket bucket.
result = bucket.list_bucket_style()
for sty in result.styles:
print('name: ' + sty.name)
print('content: ' + sty.content)Delete a specified image style
Use the following code to delete a specified image style from the examplebucket bucket.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this code, set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Set endpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Set the region that corresponds to the endpoint, such as cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace examplebucket with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Specify the name of the image style, such as imagestyle.
style = 'imagestyle'
# Delete the specified image style.
result = bucket.delete_bucket_style(style)
print('Style deleted successfully. Return status: ' + str(result.status))References
For more information about the API operation to create an image style, see PutStyle.
For more information about the API operation to query a specific image style in a bucket, see GetStyle.
For more information about the API operation to list all image styles in a bucket, see ListStyle.
For more information about the API operation to delete a specific image style in a bucket, see DeleteStyle.