O registro de acesso do Object Storage Service (OSS) captura informações detalhadas sobre as solicitações feitas aos seus buckets. Após você ativar o registro para um bucket de source, o OSS gera e entrega automaticamente arquivos de log a cada hora em um bucket de destino especificado, seguindo uma convenção de nomenclatura predefinida.
Ativar o registro de logs para um bucket
Use o código a seguir para ative o recurso de armazenamento de logs.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The China (Hangzhou) region is used as an example for the endpoint. Specify a region as needed.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before running this code, set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the bucket name, for example, examplebucket.
bucket = client.get_bucket('examplebucket')
# Set logging_bucket to the destination bucket for log files.
# Set my-log to the folder where log files are stored. If you specify this parameter, log files are saved in the specified folder. If you do not specify this parameter, log files are saved in the root directory of the destination bucket.
bucket.logging = Aliyun::OSS::BucketLogging.new(
enable: true, target_bucket: 'logging_bucket', target_prefix: 'my-log')
Consultar as configurações de registro de um bucket
Use o código a seguir para visualize a configuração de armazenamento de logs.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The China (Hangzhou) region is used as an example for the endpoint. Specify a region as needed.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before running this code, set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the bucket name, for example, examplebucket.
bucket = client.get_bucket('examplebucket')
log = bucket.logging
puts log.to_s
Desativar o armazenamento de logs
O código a seguir demonstra como desativar o registro de logs de um bucket:
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The China (Hangzhou) region is used as an example for the endpoint. Specify a region as needed.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before running this code, set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the bucket name, for example, examplebucket.
bucket = client.get_bucket('examplebucket')
bucket.logging = Aliyun::OSS::BucketLogging.new(enable: false)
Referências
Para obter mais informações sobre a operação de API que ative o registro de logs de um bucket, consulte PutBucketLogging.
Para conhecer a operação de API que consulta as configurações de registro de um bucket, consulte GetBucketLogging.
Para mais detalhes sobre a operação de API que desativa o registro de logs de um bucket, consulte DeleteBucketLogging.