如果有小伙伴使用微软的blob进行文件管理的话,可能会用到。
之前在centos参考的是:CentOS 7安装Azcopy
.net core 1.1.1的下载信息在github上:
https://github.com/dotnet/core/blob/master/release-notes/download-archives/1.1.1-download.md
在下载安装前,先装两个库:
yum install libunwind libicu
然后下载.net core的包:
curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=843420
下载后解压到制定目录:
mkdir -p /opt/dotnet && tar zxf dotnet.tar.gz -C /opt/dotnet
ln -s /opt/dotnet/dotnet /usr/local/bin
下载azcopy的包:
wget -O azcopy.tar.gz https://aka.ms/downloadazcopyprlinux
解压:
tar -xf azcopy.tar.gz
安装:
./install.sh
安装完成后,可以运行azcopy:
[root@hwmig01 ~]# azcopy
------------------------------------------------------------------------------
azcopy 6.0.0-netcorepreview Copyright (c) 2017 Microsoft Corp. All Rights Reserved.
------------------------------------------------------------------------------
# azcopy is designed for high-performance uploading, downloading, and copying
data to and from Microsoft Azure Blob, and File storage.
# Command Line Usage:
azcopy --source
会报错dotnet版本不够,需要2.x以上
那么就需要重新安装一下:https://github.com/dotnet/core/blob/master/release-notes/download-archives/2.0.0-download.md
步骤:
安装 依赖项:
Ubuntu 16.04 and Linux Mint 18
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
Fedora, CentOS and Oracle Linux
sudo yum update
sudo yum install dotnet-sdk-2.0.0
其中,sudo yum update
小心使用,之前笔者的docker重启了。。
一直报错:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libunwind
apt-get update
之后也找不到,后面在2.0文档的方法就可以了
微软官网:
https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10
参考:Python操作Azure Storage Blob经验
安装 Azure Storage SDK for Python
官方python操作文档:https://azure.github.io/azure-sdk/python_introduction.html
默认已安装好Python,已拥有Azure存储账号;
pip install azure-storage-blob
其中:
block_blob_service = BlockBlobService(account_name = 'accountname', sas_token= '连接字符串')
使用经验:代码应尽可能不放置密钥,而是使用账号+SAS(shared access signature)的方式;官方文档的SDK说明都是使用账号+密钥的连接字符串方式,其实听危险的。应该为不同的容器获取不同的SAS,设置合理的过期时间和操作权限,做好管理工作;
上传文件:
接下来我们要把本地的文件上传到刚才创建的 Blob Container 中。Azure SDK 为我们提供了下面四个方法:
create_blob_from_path #上传指定路径的文件。
create_blob_from_stream #把一个数据流中的内容上传。
create_blob_from_bytes #上传一个 bype 数组。
create_blob_from_text #使用特定的编码格式上传字符串。
是的,你没有看错,所有方法的名字中都没有 upload 字眼,而是使用了 create。这也说明上传文件的本质是在云端创建一个 Blob 对象。
from azure.storage.blob import BlockBlobService
from azure.storage.blob import ContentSettings
mystoragename = "xxxx"
mystoragekey = "yyyy"
blob_service = BlockBlobService(account_name=mystoragename, account_key=mystoragekey)
blob_service.create_blob_from_path(
'nickcon',
'myblobcortana.jpg',
'cortana-wallpaper.jpg',
content_settings=ContentSettings(content_type='image/jpg'))
这次我们引入了类型 ContentSettings,主要是指定文件的类型。注意 create_blob_from_path 方法的第二个参数,我们需要为新的 blob 对象指定一个名字。第一个参数是目标 Container, 第三个参数是要上传的本地文件路径。执行上面的脚本,会把本地的一张壁纸 cortana-wallpaper.jpg 上传到 Azure Blob Container 中:
在 Container 中创建的 Blob 对象的名称已经不是源文件的名称了,而是我们指定的 myblobcortana.jpg。
官网:https://docs.microsoft.com/zh-cn/azure/storage/common/storage-use-azcopy-blobs
azcopy \
--source "aaa/" \
--destination "link" \
--recursive
recursive代表递归,可以遍历下面的所有文件,需要包含 --recursive 选项才能传输 目录中的所有文件。
还有的人有一个resume
:
azcopy \
--source /mnt/myfiles \
--destination https://myaccount.blob.core.windows.net/mycontainer \
--dest-key key \
--resume "/mnt/myjournal"
please use --resume option to specify a custom location for the journal file.
[2020/08/04 21:38:23][ERROR] An error occurred while reading the restart journal from "/root/Microsoft/Azure/AzCopy". Detailed error: The process cannot access the file '/root/Microsoft/Azure/AzCopy/AzCopyCheckpoint.jnl' because it is being used by another process.
这边遇到了,有几个教程说:
https://joymonscode.blogspot.com/2017/10/running-multiple-copies-of-azcopy.html
An error occurred while reading the restart journal from "C:\Users\\AppData\Local\Microsoft\Azure\AzCopy". Detailed error: The process cannot access the file 'C:\Users\\AppData\Local\Microsoft\Azure\AzCopy\AzCopyCheckpoint.jnl' because it is being used by another process.
The error is pretty much clear. AzCopy keeps a journal file for resume functionality and if we don't specify the journal file location in command it uses default location and when second AzCopy starts it cannot read journal file.
The fix is to specify the location for .jnl. AzCopy Command goes as follows
AzCopy /Source:c:\temp\source /Dest:https://.blob.core.windows.net/test /DestSAS:"" /pattern:"" /s /z:
If we are running AzCopy from the command window it is easy to find out. But, if AzCopy is invoked from applications (PowerShell or .Net) in parallel it is difficult to find out because we might have disabled all the messages using /y. AzCopy has /v: switch which redirect the logs to a file. That will help to troubleshoot.
笔者直接来到/root/Microsoft/Azure/AzCopy/AzCopyCheckpoint.jnl'
,删掉了这个,就可以了。。。