Azure-CLI Blob存储删除多个Blob [英] Azure-CLI Blob Storage remove multiple blobs

查看:81
本文介绍了Azure-CLI Blob存储删除多个Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Azure-CLI删除多个文件/斑点.但是它没有被官方支持.所以我写了一个for循环,用az命令删除多个文件.看起来像这样:

I need to remove multiple files/blobs with the Azure-CLI. But it's not officially supported. So I wrote a for-loop to remove multiple files with the az command. It's look like this:

for i in `az storage blob list --container-name $BUCKET --output table | awk '{print $1}'| sed '1,2d' | sed '/^$/d'` ; do
    if az storage blob delete --name $i --container-name $BUCKET --output table ; then
      echo "Files inside the bucket (container) ${BUCKET} have been erased"
    else
      echo "Unable to erase the files inside the bucket (container) ${BUCKET}." && exit 1
    fi
done

但是这不能正常工作,我认为循环死机了.但是我不知道为什么.

But this doesn't work correctly and I think the loop hangs. But I don't know why.

我希望有人能帮助我.预先感谢.

I hope anyone can help me. Thanks in advance.

感谢您的帮助.但是什么也没发生.我正在研究Xubuntu 17.04,并重新安装了Azure cli.

Thanks for your help. But nothing happen. I'm working on Xubuntu 17.04 and had fresh installed the azure cli.

如果我按CTRL + C,则会收到此消息:

I get this Message, if I CTRL+C:

Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/applicationinsights/channel/SenderBase.py", line 118, in send
  File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 466, in open

    response = self._open(req, data)
  File "/usr/lib/python3.5/urllib/request.py", line 484, in _open
    '_open', req)
  File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 1297, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib/python3.5/urllib/request.py", line 1254, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/usr/lib/python3.5/http/client.py", line 1107, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1152, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.5/http/client.py", line 1103, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
    self.send(msg)
  File "/usr/lib/python3.5/http/client.py", line 877, in send
    self.connect()
  File "/usr/lib/python3.5/http/client.py", line 1253, in connect
    super().connect()
  File "/usr/lib/python3.5/http/client.py", line 849, in connect
(self.host,self.port), self.timeout, self.source_address)
  File "/usr/lib/python3.5/socket.py", line 703, in create_connection
    sock.connect(sa)
KeyboardInterrupt

Original exception was:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/azure/cli/core/telemetry.py", line 271, in <module>
  File "/usr/lib/python3/dist-packages/azure/cli/core/telemetry.py", line 263, in upload_telemetry
  File "/usr/lib/python3/dist-packages/applicationinsights/TelemetryClient.py", line 55, in flush
  File "/usr/lib/python3/dist-packages/applicationinsights/channel/TelemetryChannel.py", line 71, in flush
  File "/usr/lib/python3/dist-packages/applicationinsights/channel/SynchronousQueue.py", line 39, in flush
  File "/usr/lib/python3/dist-packages/applicationinsights/channel/SenderBase.py", line 118, in send
  File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 466, in open
    response = self._open(req, data)
  File "/usr/lib/python3.5/urllib/request.py", line 484, in _open
    '_open', req)
  File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 1297, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib/python3.5/urllib/request.py", line 1254, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/usr/lib/python3.5/http/client.py", line 1107, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1152, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.5/http/client.py", line 1103, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
    self.send(msg)
  File "/usr/lib/python3.5/http/client.py", line 877, in send
    self.connect()
  File "/usr/lib/python3.5/http/client.py", line 1253, in connect
    super().connect()
  File "/usr/lib/python3.5/http/client.py", line 849, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/usr/lib/python3.5/socket.py", line 703, in create_connection
    sock.connect(sa)
KeyboardInterrupt

运行此脚本的地方完全没有必要.我总是遇到相同的错误:-(

It's totaly unnecessary where I run this script. I get always the same error :-(

-aruka

推荐答案

在我的测试中,您的脚本对我有用,也许我们可以尝试以下脚本:

In my test, your script works for me, maybe we can try this script:

export AZURE_STORAGE_ACCOUNT=jasontest321
export AZURE_STORAGE_ACCESS_KEY=+v6/ZQy74QhIwdvEMbnMV7HMaDaNUAkCDizBTOVdZkBUlw==
export container_name=vhds
for i in `az storage blob list --container-name $container_name --output table | awk '{print $1}'| sed '1,2d' | sed '/^$/d'` ; do
    if az storage blob delete --name $i --container-name $container_name >/dev/null ; then
      echo "Files $i inside the $container_name have been erased"
    else
      echo "Unable to erase the files $i inside the  $container_name." && exit 1
    fi
done

以下是有关结果的屏幕截图:

Here is the screenshot about the result:

更新:

#az --version
.
role (2.0.5)
sf (1.0.2)
sql (2.0.4)
storage (2.0.7)
vm (2.0.7)

Python (Linux) 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

Python location '/root/lib/azure-cli/bin/python'
[root@jasoncli@jasonye ~]# python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

在我的实验室中,我的根帐户版本为2.7.5.我们应该为您的用户aruka检查您的python版本.

In my lab, my root account version is 2.7.5. we should check your python version for your user aruka.

Update2:
我们可以使用Azure门户云外壳来测试以下脚本:

Update2:
We can use Azure portal cloud shell to test this script:

Update3:

root@jasonvm:~# history
    1  echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ wheezy main" |      sudo tee /etc/apt/sources.list.d/azure-cli.list
    2  sudo apt-key adv --keyserver packages.microsoft.com --recv-keys 417A0893
    3  sudo apt-get install apt-transport-https
    4  sudo apt-get update && sudo apt-get install azure-cli
    5  export AZURE_STORAGE_ACCOUNT=jasondisk321
    6  export AZURE_STORAGE_ACCESS_KEY=O1/xxxWRzOlWep78iRcxxx/htgxHImKxxytlsv7gnn0hg1aGvw==
    7  export container_name=vhds
    8  for i in `az storage blob list --container-name $container_name --output table | awk '{print $1}'| sed '1,2d' | sed '/^$/d'` ; do     if az storage blob delete --name $i --container-name $container_name >/dev/null ; then       echo "Files $i inside the $container_name have been erased";     else       echo "Unable to erase the files $i inside the  $container_name." && exit 1;     fi; done
    9  az login -u jason.ye@xxxx.onmicrosoft.com -p xxxx
   10  export AZURE_STORAGE_ACCOUNT=jasondisk321
   11  export AZURE_STORAGE_ACCESS_KEY=O1/2D1UxeGcOlWep78iRcR5PxxgxxxzRbWoytlsv7gnn0hg1aGvw==
   12  export container_name=vhds
   13  for i in `az storage blob list --container-name $container_name --output table | awk '{print $1}'| sed '1,2d' | sed '/^$/d'` ; do     if az storage blob delete --name $i --container-name $container_name >/dev/null ; then       echo "Files $i inside the $container_name have been erased";     else       echo "Unable to erase the files $i inside the  $container_name." && exit 1;     fi; done
   14  export AZURE_STORAGE_ACCOUNT=jasondisk321
   15  export AZURE_STORAGE_ACCESS_KEY=O1/2D1UC0xxx/ztABmMyQFCeGcWRzOlWep78iRcR5Pt/K9yKxMT/htgxHImKqgznzRbWoytlsv7gnn0hg1aGvw==
   16  export container_name=vhds
   17  for i in `az storage blob list --container-name $container_name --output table | awk '{print $1}'| sed '1,2d' | sed '/^$/d'` ; do     if az storage blob delete --name $i --container-name $container_name >/dev/null ; then       echo "Files $i inside the $container_name have been erased";     else       echo "Unable to erase the files $i inside the  $container_name." && exit 1;     fi; done
   18  history

这篇关于Azure-CLI Blob存储删除多个Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆