首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure -文件共享中具有给定扩展名的文件

Azure -文件共享中具有给定扩展名的文件
EN

Stack Overflow用户
提问于 2020-01-27 21:58:45
回答 1查看 550关注 0票数 0

我正在使用this连接到Azure文件共享并上载文件。我想选择扩展名文件的扩展名,但我不能。我得到了一个错误显示如下。如果我删除.txt,一切都会正常工作。有没有办法在上传时指定文件扩展名?

错误:

代码语言:javascript
复制
Exception: ResourceNotFoundError: The specified parent path does not exist.

代码:

代码语言:javascript
复制
def main(blobin: func.InputStream):
    file_client = ShareFileClient.from_connection_string(conn_str="<con_string>", 
                                                            share_name="data-storage", 
                                                            file_path="outgoing/file.txt")


    f = open('/home/temp.txt', 'w+')
    f.write(blobin.read().decode('utf-8'))
    f.close()

    # Operation on file here

    f = open('/home/temp.txt', 'rb')
    string_to_upload = f.read()
    f.close()

    file_client.upload_file(string_to_upload)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-28 21:11:20

我认为您收到此错误的原因是因为您的文件服务共享中不存在outgoing文件夹。我拿了你的代码,并运行它,无论有没有扩展,在这两种情况下,我得到了相同的错误。

然后我创建了一个文件夹,并尝试上传文件,我成功地做到了。

下面是我使用的最终代码:

代码语言:javascript
复制
from azure.storage.fileshare import ShareFileClient, ShareDirectoryClient

conn_string = "DefaultEndpointsProtocol=https;AccountName=myaccountname;AccountKey=myaccountkey;EndpointSuffix=core.windows.net"

share_directory_client = ShareDirectoryClient.from_connection_string(conn_str=conn_string, 
                                                        share_name="data-storage",
                                                        directory_path="outgoing")

file_client = ShareFileClient.from_connection_string(conn_str=conn_string, 
                                                        share_name="data-storage", 
                                                        file_path="outgoing/file.txt")

# Create folder first.
# This operation will fail if the directory already exists.
print "creating directory..."
share_directory_client.create_directory()
print "directory created successfully..."

# Operation on file here
f = open('D:\\temp\\test.txt', 'rb')
string_to_upload = f.read()
f.close()

#Upload file
print "uploading file..."
file_client.upload_file(string_to_upload)
print "file uploaded successfully..."
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59932638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档