Todos los productos
Search
Centro de documentos

Object Storage Service:Estilo de imagen (SDK de Python V1)

Última actualización: Sep 22, 2026

En Object Storage Service (OSS), un estilo de imagen le permite ejecutar una serie de operaciones de procesamiento de imágenes, como escalar, recortar, rotar y agregar marcas de agua. Puede crear un estilo de imagen para definir estas operaciones y luego aplicarlo a las imágenes de un bucket para procesarlas y transformarlas rápidamente.

Notas

  • Para obtener más información sobre los parámetros de procesamiento de imágenes que admiten los estilos de imagen, consulte Image processing.

  • En este tema se utiliza el endpoint público de la región China (Hangzhou). Si desea acceder a OSS desde otros servicios de Alibaba Cloud en la misma región que OSS, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.

  • En este tema, las credenciales de acceso se obtienen de variables de entorno. Para obtener más información sobre cómo configurar las credenciales de acceso, consulte Configure access credentials using OSS SDK for Python 1.0.

  • Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.

  • Permisos

    Una cuenta de Alibaba Cloud tiene de forma predeterminada los permisos obligatorios para las operaciones de estilos de imagen. Para ejecutar estas operaciones como usuario de Resource Access Management (RAM) o mediante STS, debe contar con los permisos correspondientes:

    • Para crear un estilo de imagen, necesita el permiso oss:PutStyle.

    • Para consultar la información de un estilo de imagen específico en un bucket, necesita el permiso oss:GetStyle.

    • Para consultar todos los estilos de imagen creados en un bucket, necesita el permiso oss:ListStyle.

    • Para eliminar un estilo de imagen específico en un bucket, necesita el permiso oss:DeleteStyle.

    Para obtener más información, consulte Grant custom access policies to a RAM user.

Crear un estilo de imagen

Puede crear hasta 50 estilos de imagen para un bucket. A continuación se muestra cómo crear un estilo de imagen:

# -*- 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))

Después de crear un estilo de imagen, puede utilizarlo para procesar imágenes. Para obtener más información, consulte Use image styles to process images.

Consultar un estilo de imagen específico

A continuación se muestra cómo consultar la información de un estilo de imagen específico en un 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)

Consultar todos los estilos de imagen

A continuación se muestra cómo consultar todos los estilos de imagen creados en un 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)

Eliminar un estilo de imagen específico

Utilice el siguiente código para eliminar un estilo de imagen específico del bucket examplebucket.

# -*- 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))

Referencias

  • Para obtener más información sobre la operación de API para crear un estilo de imagen, consulte PutStyle.

  • Para obtener más información sobre la operación de API para consultar un estilo de imagen específico en un bucket, consulte GetStyle.

  • Para obtener más información sobre la operación de API para listar todos los estilos de imagen en un bucket, consulte ListStyle.

  • Para obtener más información sobre la operación de API para eliminar un estilo de imagen específico en un bucket, consulte DeleteStyle.