如何通过.py运行Blob数据传输

问题描述 投票:0回答:1

我已经尝试创建一个程序在我的VM中运行,以便可以将其从目录传输到我的Azure Blob存储帐户。每当我在程序外部(在命令行上)运行命令时,它就会起作用,但是,如果我运行的程序包含运行该命令的子进程,则该命令将不起作用。

这是我通过命令行发送的内容:

sudo ./azcopy cp "/directory/subdirectory" "https://myblob.blob.core.windows.net/container[SAS]" --recursive=true

到此完成数据传输。

当我将其放入程序中时,我遇到了许多问题。

当前代码:

import subprocess
import os
import sys
try:
    key = ('SAS')
    file_path = ('/directory/subdirectory')
    full_link = ('"https://myblob.blob.core.windows.net/' + key + '"')
    transfer = subprocess.check_output(['azcopy', 'cp', file_path,
                                        full_link,
                                        '--recursive=true'], stderr=subprocess.STDOUT)
    print('Transfer Complete.')
# except subprocess.CalledProcessError as e:
#     raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
except EOFError as error:
    #Output for EOF error, would be caused by missing SAS
    print('Error with SAS')
except Exception as e:
    #When an unexpected error has occured.
    print(str(e) + 'Unknown error has occured')
exit(0)

输出:

Command '['azcopy', 'cp', '/directory/subdirectory', '"https://myblob.blob.core.windows.net/[SAS]"', '--recursive=true']' 
returned non-zero exit status 1Unknown error has occured

如果我重新添加当前注释掉的代码中的except语句,则会出现此错误:

Traceback (most recent call last):
  File "data_transfer.py", line 11, in <module>
    '--recursive=true'], stderr=subprocess.STDOUT)
  File "/usr/lib/python3.5/subprocess.py", line 626, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.5/subprocess.py", line 708, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['azcopy', 'cp', 'datadrive/peeled-images', '"https://myblob.blob.core.windows.net[SAS]"', '--recursive=true']' returned non-zero exit status 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "data_transfer.py", line 14, in <module>
    raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
RuntimeError: command '['azcopy', 'cp', '/directory/subdirectory', '"https://myblob.blob.core.windows.net/[SAS]"', '--recursive=true']' return with error (code 1): b'\nfailed to parse user input due to error: the inferred source/destination combination is currently not supported. Please post an issue on Github if support for this scenario is desired\n'

非常感谢所有帮助

python azure subprocess blob azcopy
1个回答
0
投票
subprocess.check_output(['azcopy', 'cp', '/directory/subdirectory', full_link, '--recursive=true'], stderr=subprocess.STDOUT)

需要的更改是:

subprocess.call(['azcopy', 'cp', '/directory/subdirectory',
                 full_link, '--recursive=true'], stderr=subprocess.STDOUT)

因为这是为了运行和执行程序,不一定提供特定的输出。

© www.soinside.com 2019 - 2024. All rights reserved.