[[["容易理解","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-09-04 (世界標準時間)。"],[[["\u003cp\u003eThis 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.\u003c/p\u003e\n"],["\u003cp\u003eFiles 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 \u003ccode\u003eapp.yaml\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eServing files from a script through the App Engine \u003ccode\u003eCloudStorageTools\u003c/code\u003e 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.\u003c/p\u003e\n"],["\u003cp\u003eTo serve files directly from Cloud Storage, files must be configured as readable by anonymous users, utilizing the \u003ccode\u003epublic-read\u003c/code\u003e ACL setting and \u003ccode\u003eCloudStorageTools::getPublicUrl\u003c/code\u003e to obtain the public URL.\u003c/p\u003e\n"],["\u003cp\u003eServing files uploaded with your app can be achieved, for more details consult the link provided regarding reading and writing files.\u003c/p\u003e\n"]]],[],null,["# Providing Public Access to Files\n\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| php-gen2\n|\n| /services/access). If you are updating to the App Engine PHP 7/8 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/php-differences) to learn about your migration options for legacy bundled services.\n\n\u003cbr /\u003e\n\nA common use case is making your files publicly accessible via the web.\nYou can do this in the PHP 5 standard environment in any of these ways:\n\n- Serve files in Google Cloud Storage from a script: your app serves the file.\n- Serve files from Google Cloud Storage, which serves the file directly.\n- Serving files uploaded with your app by using the static handler in `app.yaml`.\n\nNote that the last methodology does not use Cloud Storage.\n\nServing files from a script\n---------------------------\n\nIf you want to serve files from your app, import the App Engine\n`CloudStorageTools` class: \n\n use google\\appengine\\api\\cloud_storage\\CloudStorageTools;\n\nNow use [CloudStorageTools::serve](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.cloud_storage.CloudStorageTools#method_serve)\nto serve the file from Google Cloud Storage: \n\n CloudStorageTools::serve(\"gs://${my_bucket}/serve.txt\");\n\nServing files from the app in this way allows the developer to determine user\nidentity and ensure that only authorised users access the file. The downside\nto this approach is that your application needs to run this code to serve the\nfile, which consumes instance hours and thus incurs cost.\n\nServing files directly from Google Cloud Storage\n------------------------------------------------\n\nThere is a faster and more cost-effective way to serve files than serving them\nfrom the app, as mentioned above: serving them from Cloud Storage directly via\nHTTP. The files need to be configured to be readable by anonymous users at file\nwrite time. As we'll show in the snippet below, you set the `acl` stream option\nto `public-read`.\n\nOnce the file is written to Cloud Storage as publicly readable, you need to\nget the public URL for the file, using\n[CloudStorageTools::getPublicUrl](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.cloud_storage.CloudStorageTools#method_getPublicUrl).\n\nIn the following example, we create a public-readable file containing some\nrandom numbers, writing it to a Cloud Storage bucket, and redirect to\nthat file from Cloud Storage. \n\n $options = ['gs' =\u003e ['acl' =\u003e 'public-read']];\n $context = stream_context_create($options);\n $fileName = \"gs://${my_bucket}/public_file.txt\";\n file_put_contents($fileName, $publicFileText, 0, $context);\n\n $publicUrl = CloudStorageTools::getPublicUrl($fileName, false);\n\nThe limitation of this approach is that there is no control over who can access\nthe file, because the file is readable by anyone.\n\nServing files uploaded with your app\n------------------------------------\n\nThis option is fully described under\n[Is there any other way to read and write files](/appengine/docs/legacy/standard/php/googlestorage#is_there_any_other_way_to_read_and_write_files)."]]