如何使用Python将Azure中的文件从一个存储帐户复制到另一个存储帐户?

时间:2019-08-26 04:46:27

标签: python azure azure-storage-blobs azure-storage-account

我写了一段代码,将文件从一个容器复制到同一存储帐户中的另一个容器。

  block_blob_service = BlockBlobService(
  account_name='', account_key='')

  blob_name = file_name
  copy_from_container = source
  copy_to_container = destination
  blob_url = block_blob_service.make_blob_url(copy_from_container, blob_name)
  # blob_url:https://demostorage.blob.core.windows.net/image-container/pretty.jpg
  block_blob_service.copy_blob(copy_to_container, blob_name, blob_url)

但是现在我想跨不同的存储帐户复制文件。 我该怎么办?

1 个答案:

答案 0 :(得分:1)

跨存储帐户复制Blob的基本方法大致相同。您将需要执行以下操作:

public class Computer {
    Computer[] c;
    private int id;
    private int bet;

    public void create(int numComps) {
        int i;
        c = new Computer[numComps];

        for (i = 0; i < numComps; i++) {

            c[i] = new Computer();
            c[i].id = i+1;
            c[i].bet = bet();
            c[i].display();
        }
        displayWinners();
    }

    public int bet() {
        return (int) (Math.random() * 10) + 1;
    }

    public void display() {
        String name = "Computer " + id;
        System.out.println(name + " bets " + bet);
    }

    public void displayWinners() {
        System.out.println();
    }

    public static void main(String[] args) {
        Computer c = new Computer();
        c.create(5);
    }

}

请注意,在跨存储帐户复制Blob时,应可公开访问源Blob URL。您可以通过创建对源blob至少具有读取权限的共享访问签名(SAS)URL(推荐方法),或将源blob容器的ACL(copy_from_container)的ACL设置为 source_block_blob_service = BlockBlobService( source_account_name= '', source_account_key= '') target_block_blob_service = BlockBlobService( target_account_name= '', target_account_key= '') blob_name = file_name copy_from_container = source copy_to_container = destination blob_url = source_block_blob_service.make_blob_url(copy_from_container, blob_name) # blob_url:https://demostorage.blob.core.windows.net/image-container/pretty.jpg target_block_blob_service.copy_blob(copy_to_container, blob_name, blob_url) (不推荐)。