在Cloud Functions中创建Cloud Storage CSV文件遇权限问题求助
Fixing "Read-only file system" Error When Creating CSV Files in Cloud Functions
这个问题我碰到过好多次啦——Cloud Functions的运行环境里,只有/tmp目录是支持读写操作的临时存储区,你直接在当前工作目录创建test.csv,自然会触发只读文件系统的错误。
你的原代码(问题出在这里)
def upload_blob(bucket_name, source_file_name, destination_blob_name): storage_client = storage.Client() bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_file(source_file_name) message = "Data for CSV file" csv = open('test.csv', "w") # 这里出错了! csv.write(message) with open('/tmp/test.csv', 'r') as file_obj: upload_blob('test-bucket', file_obj, 'test.csv')
你遇到的错误
OSError: [Errno 30] Read-only file system: 'test.csv'
修复方案
直接把文件创建在/tmp目录下,另外推荐用with语句来处理文件,它会自动帮你关闭文件句柄,确保写入的内容被正确刷到磁盘,避免资源泄漏:
def upload_blob(bucket_name, source_file_name, destination_blob_name): storage_client = storage.Client() bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_file(source_file_name) message = "Data for CSV file" # 直接在/tmp目录下创建并写入文件 with open('/tmp/test.csv', "w") as csv_file: csv_file.write(message) # 读取/tmp里的文件并上传到存储桶 with open('/tmp/test.csv', 'r') as file_obj: upload_blob('test-bucket', file_obj, 'test.csv')
额外小提示
/tmp目录的存储空间有限(目前Cloud Functions最多提供512MB临时存储),如果你的CSV文件很大,要注意别超限额。- 函数执行结束后,
/tmp里的临时文件会被自动清除,不用手动删除;但如果是大文件,上传完成后手动删除能提前释放资源。 - 如果要生成规范的CSV(比如处理含逗号、换行符的内容),用Python内置的
csv模块会更可靠,示例如下:
import csv def upload_blob(bucket_name, source_file_name, destination_blob_name): storage_client = storage.Client() bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_file(source_file_name) # 用csv模块生成标准CSV格式的数据 data = [["Name", "Age"], ["Alice", 30], ["Bob", 25]] with open('/tmp/test.csv', 'w', newline='') as csv_file: writer = csv.writer(csv_file) writer.writerows(data) with open('/tmp/test.csv', 'r') as file_obj: upload_blob('test-bucket', file_obj, 'test.csv')
内容的提问来源于stack exchange,提问作者TeeKay
相关产品推荐
相关产品推荐

