ファイルを公開する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
一般的なユースケースとして、ファイルをウェブで一般公開することがあります。PHP 5 スタンダード環境でこれを行うには、次のいずれかの方法を使用します。
- Google Cloud Storage のファイルをスクリプトで提供する: アプリからファイルが提供されます。
- Google Cloud Storage からファイルを提供する: ファイルが直接提供されます。
app.yaml
の静的ハンドラを使用して、アプリでアップロードされたファイルを提供します。
最後の方法では、Cloud Storage を使用しないことに注意してください。
スクリプトからファイルを提供
アプリからファイルを提供する場合は、App Engine の CloudStorageTools
クラスをインポートします。
現在は、CloudStorageTools::serve を使用して、Google Cloud Storage からファイルを提供できるようになりました。
この方法でアプリからファイルを提供することで、デベロッパーがユーザー ID を判断し、許可されたユーザーだけがファイルにアクセスできるようになります。このアプローチの欠点は、ファイルを提供するためにアプリケーションでこのコードを実行する必要があり、インスタンス時間を消費するため、コストがかさむことです。
Google Cloud Storage から直接ファイルを提供
上記のように、アプリからファイルを提供するよりも、HTTP を介して Cloud Storage から直接ファイルを提供することのほうが、迅速かつ費用対効果の高い方法です。これらのファイルは、ファイルの作成時に、匿名ユーザーが読み取れるように構成する必要があります。下記のスニペットに示すように、acl
ストリーム オプションを public-read
に設定します。
ファイルが公開設定で Cloud Storage に書き込まれたら、CloudStorageTools::getPublicUrl を使用して、ファイルの公開 URL を取得する必要があります。
次の例では、乱数が含まれる公開設定のファイルを作成し、Cloud Storage バケットに書き込み、Cloud Storage からそのファイルにリダイレクトします。
ただし、このアプローチでは誰でもファイルを読み取ることができるため、誰がファイルにアクセスできるかを制御できないという制約があります。
アプリでアップロードされたファイルの提供
このオプションの詳細については、ファイルを読み書きするその他の方法をご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-03-13 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-03-13 UTC。"],[[["This API supports first-generation runtimes and is applicable when upgrading to corresponding second-generation runtimes, while migration to App Engine PHP 7/8 runtime requires consulting the migration guide."],["Files can be made publicly accessible via the web in the PHP 5 standard environment by serving them from a script, serving them directly from Google Cloud Storage, or using the static handler in `app.yaml`."],["Serving files from a script through the App Engine `CloudStorageTools` class allows for user identity verification, but it consumes instance hours, and serving directly from Cloud Storage is faster and more cost-effective, but it lacks user access control."],["To serve files directly from Cloud Storage, files must be configured as readable by anonymous users, utilizing the `public-read` ACL setting and `CloudStorageTools::getPublicUrl` to obtain the public URL."],["Serving files uploaded with your app can be achieved, for more details consult the link provided regarding reading and writing files."]]],[]]