Todos os produtos
Search
Central de documentação

Object Storage Service:Image styles (Python SDK V1)

Última atualização: Sep 22, 2026

No Object Storage Service (OSS), um estilo de imagem permite executar uma série de operações de processamento, como dimensionamento, recorte, rotação e inserção de marcas d'água. Crie um estilo de imagem para definir essas operações e aplique-o às imagens em um bucket para processá-las e transformá-las rapidamente.

Observações

  • Para obter mais informações sobre os parâmetros de processamento de imagem suportados pelos estilos de imagem, consulte Image processing.

  • Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região do OSS, use um endpoint interno. Para obter mais informações sobre as regiões e endpoints do OSS, consulte Regions and endpoints.

  • Neste tópico, você obtém as credenciais de acesso a partir de variáveis de ambiente. Para obter mais informações sobre como configurar credenciais de acesso, consulte Configure access credentials using OSS SDK for Python 1.0.

  • Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou se autenticar com credenciais do Security Token Service (STS), consulte Initialization.

  • Permissões

    Por padrão, uma conta Alibaba Cloud tem as permissões necessárias para operações com estilos de imagem. Para executar essas operações como usuário do Resource Access Management (RAM) ou por meio do STS, você precisa ter as permissões correspondentes:

    • Para criar um estilo de imagem, você precisa da permissão oss:PutStyle.

    • Para consultar as informações de um estilo específico em um bucket, é preciso ter a permissão oss:GetStyle.

    • Para listar todos os estilos de imagem de um bucket, você deve possuir a permissão oss:ListStyle.

    • Para excluir um estilo específico em um bucket, a permissão oss:DeleteStyle é obrigatória.

    Para obter mais informações, consulte Grant custom access policies to a RAM user.

Crie um estilo de imagem

Você pode criar até 50 estilos de imagem por bucket. O código a seguir mostra como criar um estilo de imagem:

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

Depois de criar um estilo de imagem, use-o para processar imagens. Para obter mais informações, consulte Use image styles to process images.

Consulte um estilo de imagem específico

O código a seguir mostra como consultar informações sobre um estilo de imagem específico em um 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)

Consulte todos os estilos de imagem

O código a seguir mostra como consultar todos os estilos de imagem criados em um 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)

Exclua um estilo de imagem específico

Use o código a seguir para excluir um estilo de imagem específico do 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))

Referências

  • Para obter mais informações sobre a operação de API para criar um estilo de imagem, consulte PutStyle.

  • Para detalhes sobre a operação de API de consulta de um estilo específico em um bucket, consulte GetStyle.

  • Para mais informações sobre a operação de API que lista todos os estilos de imagem em um bucket, consulte ListStyle.

  • Consulte DeleteStyle para saber mais sobre a operação de API que exclui um estilo específico em um bucket.