すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:バケット在庫

最終更新日:Nov 08, 2024

このトピックでは、バケットのインベントリを作成する方法と、バケットに設定されたインベントリを照会、一覧表示、および削除する方法について説明します。

使用上の注意

  • このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。

  • このトピックでは、アクセス資格情報は環境変数から取得します。 アクセス資格情報の設定方法の詳細については、「アクセス資格情報の設定」をご参照ください。

  • このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。

  • バケットのインベントリを作成、表示、一覧表示、および削除する権限があることを確認します。 デフォルトでは、バケット所有者に上記の操作を実行する権限があります。 上記の操作を実行する権限がない場合は、バケットの所有者に権限を付与するよう依頼してください。

  • バケットには最大1,000のインベントリを設定できます。

  • インベントリを構成するソースバケットは、インベントリリストが保存されているターゲットバケットと同じリージョンにデプロイする必要があります。

バケットのインベントリを作成する

次のコードは、バケットのインベントリを作成する方法の例を示しています。

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import (InventoryConfiguration,
                         InventoryFilter,
                         InventorySchedule,
                         InventoryDestination,
                         InventoryBucketDestination,
                         INVENTORY_INCLUDED_OBJECT_VERSIONS_CURRENT,
                         INVENTORY_FREQUENCY_DAILY,
                         INVENTORY_FORMAT_CSV,
                         FIELD_SIZE,
                         FIELD_LAST_MODIFIED_DATE,
                         FIELD_STORAG_CLASS,
                         FIELD_ETAG,
                         FIELD_IS_MULTIPART_UPLOADED,
                         FIELD_ENCRYPTION_STATUS)

# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
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 the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the ID of the account to which the bucket owner grants permissions to perform the operation. Example: 1283641033516515.
account_id = 'yourtBucketDestinationAccountId'
# Specify the name of the RAM role that is granted permissions to read all objects in the source bucket for which you want to configure the inventory and permissions to write data to the destination bucket in which you want to store the generated inventory lists. Example: acs:ram::1283641033516515:role/AliyunOSSRole.
role_arn = 'yourBucketDestinationRoleArn'
# Specify the name of the bucket in which you want to store the generated inventory lists. 
dest_bucket_name = 'yourDestinationBucketName'

# Specify the inventory name. 
inventory_id = "inventory1"

# Specify the object attributes that are included in the inventory lists. 
optional_fields = [FIELD_SIZE, FIELD_LAST_MODIFIED_DATE, FIELD_STORAG_CLASS,
                   FIELD_ETAG, FIELD_IS_MULTIPART_UPLOADED, FIELD_ENCRYPTION_STATUS]

# Create the bucket in which you want to store the generated inventory lists. 
bucket_destination = InventoryBucketDestination(
    # Specify the ID of the account to which the destination bucket belongs. 
    account_id=account_id,
    # Specify the ARN of the RAM role that you want to use to access the destination bucket. 
    role_arn=role_arn,
    # Specify the name of the destination bucket. 
    bucket=dest_bucket_name,
    # Specify the format of the inventory lists. 
    inventory_format=INVENTORY_FORMAT_CSV,
    # Specify the prefix of the path in which you want to store the generated inventory lists. 
    prefix='destination-prefix',
    # The following code provides an example on how to encrypt inventory lists by using customer master keys (CMKs) that are managed by Key Management Service (KMS). 
    # sse_kms_encryption=InventoryServerSideEncryptionKMS("test-kms-id"),
    # The following code provides an example on how to encrypt the inventory lists on the OSS server. 
    # sse_oss_encryption=InventoryServerSideEncryptionOSS()
)

# Create an inventory. 
inventory_configuration = InventoryConfiguration(
    # Specify the ID of the inventory. 
    inventory_id=inventory_id,
    # Specify whether to enable the inventory for the bucket. Valid values: true and false. 
    is_enabled=True,
    # Specify whether to generate inventory lists on a daily or weekly basis. The following code provides an example on how to generate inventory lists on a daily basis. A value of Weekly indicates that inventory lists are generated on a weekly basis. A value of Daily indicates that inventory lists are generated on a daily basis. 
    inventory_schedule=InventorySchedule(frequency=INVENTORY_FREQUENCY_DAILY),
    # Specify that inventory lists include only the current versions of objects. If you set the INVENTORY_INCLUDED_OBJECT_VERSIONS parameter to ALL, all versions of objects are included in inventory lists. This inventory takes effect only when versioning is enabled for the source bucket. 
    included_object_versions=INVENTORY_INCLUDED_OBJECT_VERSIONS_CURRENT,
    # Specify the name prefix of objects that you want to include in inventory lists. 
    # inventory_filter=InventoryFilter(prefix="obj-prefix"),
    # Specify conditions to filter inventory lists. For example, the beginning of the time range during which the object was last modified is 1637883649. 
    inventory_filter=InventoryFilter(
        # Specify the prefix that is used to filter inventories. 
        "obj-prefix",
        # Specify the beginning of the time range during which the object was last modified. Unit: seconds. 
        1637883649,
        # Specify the end of the time range during which the object was last modified. Unit: seconds. 
        1638347592,
        # Specify the minimum size of the object. Unit: B. 
        1024,
        # Specify the maximum size of the object. Unit: B. 
        1048576,
        # Specify the storage classes of objects. 
        'Standard,IA'),
    # Specify the object attributes that you want to include in inventory lists. 
    optional_fields=optional_fields,    
    inventory_destination=InventoryDestination(bucket_destination=bucket_destination))

# Apply the inventory to the bucket. 
result = bucket.put_bucket_inventory_configuration(inventory_configuration)
print(result.status)

バケットのインベントリの照会

次のコードは、バケットに設定されたインベントリを照会する方法の例を示しています。

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os

# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
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 the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the inventory name. 
inventory_id = "inventory1"

# Query the inventory. 
result = bucket.get_bucket_inventory_configuration(inventory_id=inventory_id)

# Display information about the inventory. 
print('======inventory configuration======')
print('inventory_id', result.inventory_id)
print('is_enabled', result.is_enabled)
print('frequency', result.inventory_schedule.frequency)
print('included_object_versions', result.included_object_versions)
print('inventory_filter prefix', result.inventory_filter.prefix)
print('fields', result.optional_fields)
bucket_destin = result.inventory_destination.bucket_destination
print('===bucket destination===')
print('account_id', bucket_destin.account_id)
print('role_arn', bucket_destin.role_arn)
print('bucket', bucket_destin.bucket)
print('format', bucket_destin.inventory_format)
print('prefix', bucket_destin.prefix)
if bucket_destin.sse_kms_encryption is not None:
    print('server side encryption by kms, key id:', bucket_destin.sse_kms_encryption.key_id)
elif bucket_destin.sse_oss_encryption is not None:
    print('server side encryption by oss.')

一度に複数の在庫をリストする

説明

1回のリクエストで最大100のインベントリを照会できます。 100を超えるインベントリをクエリするには、複数のリクエストを送信し、各リクエストに対して返されたトークンを次のリクエストのパラメーターとして使用する必要があります。

次のコードは、バケットに設定されたインベントリを一覧表示する方法の例です。

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os

# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
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 the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Display information about the inventories. 
def print_inventory_configuration(configuration):
    print('======inventory configuration======')
    print('inventory_id', configuration.inventory_id)
    print('is_enabled', configuration.is_enabled)
    print('frequency', configuration.inventory_schedule.frequency)
    print('included_object_versions', configuration.included_object_versions)
    print('inventory_filter prefix', configuration.inventory_filter.prefix)
    print('fields', configuration.optional_fields)
    bucket_destin = configuration.inventory_destination.bucket_destination
    print('===bucket destination===')
    print('account_id', bucket_destin.account_id)
    print('role_arn', bucket_destin.role_arn)
    print('bucket', bucket_destin.bucket)
    print('format', bucket_destin.inventory_format)
    print('prefix', bucket_destin.prefix)
    if bucket_destin.sse_kms_encryption is not None:
        print('server side encryption by kms, key id:', bucket_destin.sse_kms_encryption.key_id)
    elif bucket_destin.sse_oss_encryption is not None:
        print('server side encryption by oss.')

# List all inventories. 
# If the number of inventories exceeds 100, the results are returned by page. The pagination information is stored in the <oss2.models.ListInventoryConfigurationResult> class. 
continuation_token = None
while 1:
    result = bucket.list_bucket_inventory_configurations(continuation_token=continuation_token)
    # Obtain whether the results are listed by page. 
    print('is truncated', result.is_truncated)
    # Obtain the token to carry in this list. 
    print('continuaiton_token', result.continuaiton_token)
    # Obtain the token to carry in the subsequent list. 
    print('next_continuation_token', result.next_continuation_token)

    # Display information about the inventories. 
    for inventory_config in result.inventory_configurations:
        print_inventory_configuration(inventory_config)

    # If the results are listed by page, continue the listing. The subsequent list must carry the continuation token. 
    if result.is_truncated:
        continuation_token = result.next_continuation_token
    else:
         break

バケットのインベントリを削除する

次のコードは、バケットに設定されたインベントリを削除する方法の例を示しています。

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os

# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
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 the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the inventory name. 
inventory_id = "inventory1"

# Delete the inventory. 
bucket.delete_bucket_inventory_configuration(inventory_id)

関連ドキュメント

  • バケットインベントリを管理するための完全なサンプルコードについては、『GitHub』をご参照ください。

  • バケットのインベントリを作成するためのAPI操作の詳細については、「PutBucketInventory」をご参照ください。

  • バケットに設定されたインベントリを照会するためのAPI操作の詳細については、「GetBucketInventory」をご参照ください。

  • バケットに設定されたインベントリを一覧表示するAPI操作の詳細については、「ListBucketInventory」をご参照ください。

  • バケットに設定されたインベントリを削除するAPI操作の詳細については、「DeleteBucketInventory」をご参照ください。