要将内容直接上传到 Google Cloud Storage,您需要对特定网址发出 HTTP POST 请求(稍后我们将对此进行介绍)。App Engine 随后使用特定的上传服务来处理该 POST 并将文件写入 Google Cloud Storage。文件写入完成后,App Engine 会通知您的应用上传已完成。由于只有在上传完成后才会调用您的应用,因此您可以使用此方法上传非常大的文件(可达到当前大小上限 100 TB)。
用户将文件直接上传到 Google Cloud Storage 比从 App Engine 应用写入 Google Cloud Storage 更快、更具成本效益,因为这会耗费实例小时数并产生费用。而且,向应用发出请求期间,不会执行文件写入操作。因此,直接上传文件时,您不会像其他情况那样受到请求超时的限制,并可上传非常大的文件。
实现文件上传
要实现用户文件上传,请执行以下操作:
导入 CloudStorageTools:
use google\appengine\api\cloud_storage\CloudStorageTools;
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eThe \u003ccode\u003eREGION_ID\u003c/code\u003e is a Google-assigned code based on the region selected during app creation, which is included in App Engine URLs for apps created after February 2020, and it does not directly correspond to a country or province.\u003c/p\u003e\n"],["\u003cp\u003eUploading files directly to Google Cloud Storage via HTTP POST is faster and more cost-effective than writing to it from the App Engine app because it avoids instance hour consumption and request timeouts, supporting files up to 100 Terabytes.\u003c/p\u003e\n"],["\u003cp\u003eTo implement user file uploads, you must import \u003ccode\u003eCloudStorageTools\u003c/code\u003e and use the \u003ccode\u003eCloudStorageTools::createUploadUrl()\u003c/code\u003e method to generate a unique upload URL that must be used within 10 minutes.\u003c/p\u003e\n"],["\u003cp\u003eUpon successful file upload, a POST request is sent to the specified handler, and the PHP runtime populates the \u003ccode\u003e$_FILES\u003c/code\u003e array, with \u003ccode\u003etmp_filename\u003c/code\u003e pointing to the uploaded file's location in Google Cloud Storage.\u003c/p\u003e\n"],["\u003cp\u003eFiles that are not moved using \u003ccode\u003emove_uploaded_file\u003c/code\u003e or \u003ccode\u003erename\u003c/code\u003e before the request finishes will be automatically removed from Google Cloud Storage.\u003c/p\u003e\n"]]],[],null,["# Allowing Users to Upload Files\n\n### Region ID\n\nThe \u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e is an abbreviated code that Google assigns\nbased on the region you select when you create your app. The code does not\ncorrespond to a country or province, even though some region IDs may appear\nsimilar to commonly used country and province codes. For apps created after\nFebruary 2020, \u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e`.r` is included in\nApp Engine URLs. For existing apps created before this date, the\nregion ID is optional in the URL.\n\nLearn more\n[about region IDs](/appengine/docs/legacy/standard/php/how-requests-are-routed#region-id). \nOK\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\nWhen you upload directly to Google Cloud Storage, you make an HTTP POST to a specific URL, which we'll\ndescribe in a moment. App Engine then uses a specific *upload service* to handle\nthe post and write the file to Google Cloud Storage. When the file write is complete, App Engine\nnotifies your app that the upload is complete. Because your app is invoked only\nupon completion, you can use this method to upload very large files, up to\nthe current maximum of 100 Terabytes.\n\nUser upload of files directly to Google Cloud Storage is faster and more cost-effective than\n[writing to Google Cloud Storage](/appengine/docs/legacy/standard/php/googlestorage#writing_files_from_your_app)\nfrom your App Engine app, because this consumes instance hours and incurs cost. Moreover, the file\nwrite does not occur within a request to the application. Therefore it is exempt\nfrom the [request timeout](/appengine/docs/legacy/standard/php/how-instances-are-managed#timeout)\nthat would otherwise apply and allows uploads of very large files.\n| **Note:** Starting from version 1.9.18, you can also upload files directly to your POST handler as long as the combined size is less than 32MB. See [this page](https://gae-php-tips.appspot.com/2015/03/09/direct-file-uploads-for-php-5-5/) for more details.\n\nImplementing file uploads\n-------------------------\n\nTo implement user file upload:\n\n1. Import `CloudStorageTools`:\n\n use google\\appengine\\api\\cloud_storage\\CloudStorageTools;\n\n2. Create the application specific upload URL, using the method\n [CloudStorageTools::createUploadUrl()](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.cloud_storage.CloudStorageTools#method_createUploadUrl)\n in your `.php` file as follows:\n\n $options = ['gs_bucket_name' =\u003e $my_bucket];\n $upload_url = CloudStorageTools::createUploadUrl('/upload/handler', $options);\n\n See [`createUploadUrl` options](#createuploadurl_options) for details about\n available options. Note that `my_bucket` will be\n `YOUR_APP_ID`.\n \u003cvar translate=\"no\"\u003e\u003ca href=\"#appengine-urls\" style=\"border-bottom: 1px dotted #999\" class=\"devsite-dialog-button\" data-modal-dialog-id=\"regional_url\" track-type=\"progressiveHelp\" track-name=\"modalHelp\" track-metadata-goal=\"regionalURL\"\u003eREGION_ID\u003c/a\u003e\u003c/var\u003e`.r.appspot.com` if using the default bucket.\n3. Note that you must start uploading to this URL within 10 minutes of its\n creation. Also, you cannot change the URL in any way - it is signed and the\n signature is checked before your upload begins.\n\n4. Use this URL as the action for the form you use to accept uploads, for example:\n\n \u003cform action=\"{{ upload_url }}\" enctype=\"multipart/form-data\" method=\"post\"\u003e\n Files to upload: \u003cbr\u003e\n \u003cinput type=\"file\" name=\"uploaded_files\" size=\"40\"\u003e\n \u003cinput type=\"submit\" value=\"Send\"\u003e\n \u003c/form\u003e\n\nAfter the file(s) upload, a POST is made to the path specified as the first\nparameter to `createUploadUrl`; in the example above, this is\n`/upload_handler.php`. The PHP runtime forms the correct `$_FILES` super global,\nand `tmp_filename` refers to the filename of the newly uploaded file in Google Cloud Storage.\n\nFor example, suppose the content of upload_handler.php is the following: \n\n var_dump($_FILES);\n\nUploading a file called `hello.txt` might result in the following output: \n\n array(1) {\n ['uploaded_files']=\u003e\n array(5) {\n ['name']=\u003e string(14) 'hello.txt'\n ['type']=\u003e string(10) 'text/plain'\n ['tmp_name']=\u003e string(73) 'gs://my_bucket/L2FwcHMtdXBsb2FkL2Jsb2JzL2IxNUFBVGNJNXNTd0VqR0tFSUtDRGxadGc'\n ['error']=\u003e int(0)\n ['size']=\u003e int(1452)\n }\n }\n\nAfter the upload is complete, you can read the uploaded file using the\n`gs://` stream wrapper. You use `move_uploaded_file` like you normally would for\nany other uploaded file, for example: \n\n $file_name = $_FILES['uploaded_files']['name'];\n $temp_name = $_FILES['uploaded_files']['tmp_name'];\n move_uploaded_file($temp_name, \"gs://${my_bucket}/${file_name}.txt\");\n\n| **Note:** Any user uploaded files that are not moved using `move_uploaded_file` or `rename` before the request finishes will be automatically removed from Google Cloud Storage.\n\n`createUploadUrl` options\n-------------------------\n\nValid `createUploadUrl` options are shown in the following table:"]]