If you want to perform operations on an image that are stored in Object Storage Service (OSS), such as resizing an image, cropping an image, rotating an image, and adding a watermark to an image, you can create image styles in OSS and define image processing operations in the image styles. You can perform all operations that are defined in the image styles on all images in a bucket.
Usage notes
For information about the image processing parameters that are supported by image styles, see IMG parameters.
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, endpoints and open ports.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
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.
Required permissions
By default, an Alibaba Cloud account has the permissions to create and use image styles. If you want to create and use image styles by using a RAM user or Security Token Service (STS), take note of the following items:
To create an image style, you must have the
oss:PutStyle
permission.To query a specific image style of a bucket, you must have the
oss:GetStyle
permission.To query all image styles of a bucket, you must have the
oss:ListStyle
permission.To delete a specific image style of a bucket, you must have the
oss:DeleteStyle
permission.
For more information, see Attach a custom policy to a RAM user.
Create an image style
You can create up to 50 image styles for a bucket. The following code provides an example on 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 in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your 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 ('Style created. Returned status:' + str(result.status))
After you create an image style, you can use the image style to process images. For more information, see Use an image style to process images.
Query a specific image style
The following code provides an example on how to query a specific image style of a bucket:
# -*- 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 in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Specify the name of the image style, such as imagestyle.
style = 'imagestyle'
# Query the image style of examplebucket.
result = bucket.get_bucket_style(style)
print('name:' + result.name)
print('content:' + result.content)
Query all image styles
The following code provides an example on how to query all image styles of a bucket:
# -*- 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 in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Query all image styles of examplebucket.
result = bucket.list_bucket_style()
for sty in result.styles:
print('name:' + sty.name)
print('content:' + sty.content)
Delete a specific image style
The following code provides an example on how to delete a specific image style of the examplebucket bucket:
# -*- 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 in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
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. Returned status:' + str(result.status))
References
For more information about the API operation that you can call to create an image style, see PutStyle.
For more information about the API operation that you can call to query a specific image style of a bucket, see GetStyle.
For more information about the API operation that you can call to query all image styles of a bucket, see ListStyle.
For more information about the API operation that you can call to delete a specific image style of a bucket, see DeleteStyle.