文件系统
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
演示如何访问 Cloud Functions 函数实例的文件系统。
代码示例
C#
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Go
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Java
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Node.js
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
PHP
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Python
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Ruby
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。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)."]]