合成スクリプトでの Python パッケージのインポート

合成スクリプトは、事前にインストールされた Python パッケージを使用できる存続期間の短いコンテナで実行されます。スクリプトはコンテナおよびコンテナの寿命に制限されます。

事前インストールされたライブラリ

次のモジュールはコンテナにインストールされていて、合成スクリプトにインポートすることによってアクセスできます。

注:
  • ‘os’、‘socket’、‘subprocess’ の各モジュールは、新しいジョブに対して許可されません。
  • 既存のジョブはすべて引き続き実行されます。

SFTP を使用したファイルのアップロードとダウンロード

これらのコンテナにインストールされているパッケージの 1 つに pysftp があります。SFTP を使用してファイルをアップロードおよびダウンロードするために使用することができます。

次のコード例は、パッケージの基本機能を示しています。

import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
# The <hostname> can be in either of the following formats: ftp.domain.ca || http://ftp.domain.ca
with pysftp.Connection('<hostname>', username='xxxx', password='xxxxxx', cnopts=cnopts) as sftp:
# Change to the dictory 'public'
with sftp.cd('public'):
# Fetch the remote file
sftp.get('<remote_file_name>')
# Upload the same file to a remote location
sftp.put('<local_file_name>')

HTTP 要求の作成

HTTP ライブラリ要求を使用できます。ライブラリの機能と使用に関する詳細については、「要求:Humans™ の HTTP」を参照してください。

import requests
r = requests.get('https://api.github.com/events?per_page=1')
print("Status Code: %s\n" %(r.status_code))
print ("Headers: \n%s\n" %(r.headers))
print("Response: \n%s" %(r.text))