檔案系統
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
說明如何存取 Cloud Functions 執行個體的檔案系統。
程式碼範例
Java
如要驗證 Cloud Run 函式,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Node.js
如要驗證 Cloud Run 函式,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Python
如要驗證 Cloud Run 函式,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Ruby
如要驗證 Cloud Run 函式,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page demonstrates how to access the file system of a Cloud Functions instance.\u003c/p\u003e\n"],["\u003cp\u003eCode examples are provided in C#, Go, Java, Node.js, PHP, Python, and Ruby to illustrate file system access.\u003c/p\u003e\n"],["\u003cp\u003eApplication Default Credentials are required to authenticate to Cloud Run functions, as detailed in the provided link.\u003c/p\u003e\n"],["\u003cp\u003eEach example shows how to list the files present in the function's current directory.\u003c/p\u003e\n"]]],[],null,["# File system\n\nShows how to access a Cloud Functions instance's file system.\n\nCode sample\n-----------\n\n### C#\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n using Google.Cloud.Functions.Framework;\n using Microsoft.AspNetCore.Http;\n using System.IO;\n using System.Threading.Tasks;\n\n namespace FileSystem;\n\n public class Function : IHttpFunction\n {\n public async Task HandleAsync(HttpContext context)\n {\n string[] files = Directory.GetFiles(\".\");\n\n await context.Response.WriteAsync(\"Files:\\n\", context.RequestAborted);\n foreach (string file in files)\n {\n await context.Response.WriteAsync($\"\\t{file}\\n\", context.RequestAborted);\n }\n }\n }\n\n### Go\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Package tips contains tips for writing Cloud Functions in Go.\n package tips\n\n import (\n \t\"fmt\"\n \t\"log\"\n \t\"net/http\"\n \t\"os\"\n\n \t\"github.com/GoogleCloudPlatform/functions-framework-go/functions\"\n )\n\n func init() {\n \tfunctions.HTTP(\"ListFiles\", ListFiles)\n }\n\n // ListFiles lists the files in the current directory.\n // Uses directory \"serverless_function_source_code\" as defined in the Go\n // Functions Framework Buildpack.\n // See https://github.com/GoogleCloudPlatform/buildpacks/blob/56eaad4dfe6c7bd0ecc4a175de030d2cfab9ae1c/cmd/go/functions_framework/main.go#L38.\n func ListFiles(w http.ResponseWriter, r *http.Request) {\n \tfiles, err := os.ReadDir(\"./serverless_function_source_code\")\n \tif err != nil {\n \t\thttp.Error(w, \"Unable to read files\", http.StatusInternalServerError)\n \t\tlog.Printf(\"ListFiles: %v\", err)\n \t\treturn\n \t}\n \tfmt.Fprintln(w, \"Files:\")\n \tfor _, f := range files {\n \t\tfmt.Fprintf(w, \"\\t%v\\n\", f.Name())\n \t}\n }\n\n### Java\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.functions.HttpFunction;\n import com.google.cloud.functions.HttpRequest;\n import com.google.cloud.functions.HttpResponse;\n import java.io.File;\n import java.io.IOException;\n import java.io.PrintWriter;\n\n public class FileSystem implements HttpFunction {\n\n // Lists the files in the current directory.\n @Override\n public void service(HttpRequest request, HttpResponse response)\n throws IOException {\n File currentDirectory = new File(\".\");\n File[] files = currentDirectory.listFiles();\n PrintWriter writer = new PrintWriter(response.getWriter());\n writer.println(\"Files:\");\n for (File f : files) {\n writer.printf(\"\\t%s%n\", f.getName());\n }\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const fs = require('fs');\n const functions = require('@google-cloud/functions-framework');\n\n /**\n * HTTP Cloud Function that lists files in the function directory\n *\n * @param {Object} req Cloud Function request context.\n * @param {Object} res Cloud Function response context.\n */\n functions.http('listFiles', (req, res) =\u003e {\n fs.readdir(__dirname, (err, files) =\u003e {\n if (err) {\n console.error(err);\n res.sendStatus(500);\n } else {\n console.log('Files', files);\n res.sendStatus(200);\n }\n });\n });\n\n### PHP\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n use Psr\\Http\\Message\\ServerRequestInterface;\n\n function listFiles(ServerRequestInterface $request): string\n {\n $contents = scandir(__DIR__);\n\n $output = 'Files:' . PHP_EOL;\n\n foreach ($contents as $file) {\n $output .= \"\\t\" . $file . PHP_EOL;\n }\n\n return $output;\n }\n\n### Python\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import functions_framework\n\n\n @functions_framework.http\n def list_files(request):\n import os\n from os import path\n\n root = path.dirname(path.abspath(__file__))\n children = os.listdir(root)\n files = [c for c in children if path.isfile(path.join(root, c))]\n return f\"Files: {files}\"\n\n### Ruby\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n require \"functions_framework\"\n\n FunctionsFramework.http \"concepts_filesystem\" do |_request|\n files = Dir.entries \".\"\n \"Files: #{files.join \"\\n\"}\"\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=functions)."]]