Sistem file
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Menunjukkan cara mengakses sistem file instance Cloud Functions.
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","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)."]]