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

Simple Log Service:SLS Python SDK の初期化

最終更新日:Jun 04, 2026

LogClient は、プロジェクトや Logstore の管理、ログの書き込みおよび読み取りを行うための Simple Log Service (SLS) Python SDK クライアントです。リクエストの送信を開始するには、LogClient インスタンスを初期化し、必要に応じてデフォルト設定を変更してください。

前提条件

LogClient の初期化

API

class LogClient(object):
    """ Construct the LogClient with endpoint, accessKeyId, accessKey.

    :type endpoint: string
    :param endpoint: log service host name, for example, ch-hangzhou.log.aliyuncs.com or https://cn-beijing.log.aliyuncs.com

    :type accessKeyId: string
    :param accessKeyId: aliyun accessKeyId

    :type accessKey: string
    :param accessKey: aliyun accessKey
    """

    __version__ = API_VERSION
    Version = __version__

    def __init__(self, endpoint, accessKeyId=None, accessKey=None, securityToken=None, source=None,
                 auth_version=AUTH_VERSION_1, region='', credentials_provider=None):

リクエストパラメーター

変数

必須

説明

endpoint

String

はい

SLS エンドポイント。前提条件セクションから取得してください。

cn-hangzhou.log.aliyuncs.com

accessKeyId

String

はい

  • AccessKey ペアを使用する場合、Alibaba Cloud アカウントまたは RAM ユーザーの AccessKey ID であり、呼び出し元を識別するために使用されます。アクセス認証情報の設定

    警告

    Alibaba Cloud アカウント (root ユーザー) の AccessKey ペアはすべてのリソースに対して完全な権限を持ちます。漏洩した場合、システムは重大なリスクにさらされます。必要な最小限の権限を持つ RAM ユーザーの AccessKey ペアを使用してください。

  • STS トークンを使用する場合、AssumeRole によって返される `Credentials` 内の `AccessKeyId` です。

LTAI****************

accessKey

String

はい

  • AccessKey ペアを使用する場合、Alibaba Cloud アカウントまたは RAM ユーザーの AccessKey Secret であり、AccessKey ID の検証に使用されます。アクセス認証情報の設定

  • STS トークンを使用する場合、AssumeRole によって返される `Credentials` 内の `AccessKeySecret` です。

yourAccessKeySecret

securityToken

String

いいえ

  • RAM ロールの一時的な認証情報です。STS トークンを使用する場合のみ必要です。AssumeRole によって返される `Credentials` 内の `SecurityToken` に対応します。

  • セキュリティトークンの長さは可変です。最大長の制限を設けないでください。AssumeRole - RAM ロールの一時的な ID 認証情報を取得

****************

source

String

いいえ

ログソース識別子。このパラメーターを空のままにするか、指定しない場合、デフォルトでローカルサーバーの IP アドレスが使用されます。

10.105.214.**

auth_version

String

いいえ

  • 認証プロトコルのバージョン。有効値:AUTH_VERSION_1 および AUTH_VERSION_4。デフォルト値:AUTH_VERSION_1

  • AUTH_VERSION_4 はより安全です。これを使用する場合は、`region` も指定する必要があります。

AUTH_VERSION_1

region

String

いいえ

プロジェクトのリージョン。サポートされているリージョン

cn-hangzhou

AccessKey ペアを使用した初期化 (V4 署名)

# Import the sls package.
from aliyun.log import *
from aliyun.log.auth import AUTH_VERSION_4

# To obtain the AccessKey pair from environment variables, import the os library.
import os

# Obtain the AccessKey ID and AccessKey secret from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')

# The endpoint of Simple Log Service.
endpoint = "yourEndpoint"

# Create a LogClient instance. Use V4 signature and specify the region based on your requirements. This example uses the China (Hangzhou) region.
client = LogClient(endpoint, access_key_id, access_key_secret, auth_version=AUTH_VERSION_4, region='cn-hangzhou')

AccessKey ペアを使用した初期化 (V1 署名)

# Import the sls package.
from aliyun.log import *

# To obtain the AccessKey pair from environment variables, import the os library.
import os

# Obtain the AccessKey ID and AccessKey secret from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')

# The endpoint of Simple Log Service.
endpoint = "yourEndpoint"

# Create a LogClient instance.
client = LogClient(endpoint, access_key_id, access_key_secret)

STS を使用した初期化

# Import the sls package.
from aliyun.log import *

# To obtain credentials from environment variables, import the os library.
import os

# Obtain the AccessKey ID, AccessKey secret, and STS token from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
securityToken = os.environ.get('ALIBABA_CLOUD_STS_TOKEN')
# The endpoint of Simple Log Service.
endpoint = "yourEndpoint"

# Create a LogClient instance.
client = LogClient(endpoint, access_key_id, access_key_secret, securityToken)

次のステップ

  • LogClient を初期化したら、API オペレーションを使用してプロジェクトを作成したり、ログを書き込んだりできます。Python SDK クイックスタート