none
求azure blob示例代码(C++) RRS feed

答案

  • Hi,

    我这没有C++的开发环境,不过你可以尝试下下面的代码,在尝试代码前,请到NutGet中下载c++版的Azure Storage程序集。

    创建blob:

    void CreateTextBlobs()
    {
      auto storage_account = cloud_storage_account::parse(
          U("UseDevelopmentStorage=true"));
      auto blob_client = storage_account.create_cloud_blob_client();
      auto container = blob_client.get_container_reference(
          U("textdata"));
      bool created = container.create_if_not_exists();
    
      blob_container_permissions permissions;
      permissions.set_public_access(
          blob_container_public_access_type::container);
      container.upload_permissions(permissions);
    
      auto text_blob1 = container.get_block_blob_reference(
          U("texts/text1"));
      text_blob1.upload_text(U("This is some text - modified"));
    
      auto text_blob2 = container.get_block_blob_reference(
          U("texts/text2"));
      text_blob2.upload_from_file(U("./stdafx.h"));
    }

    列出blob

    void ListTextBlobs()
    {
      auto storage_account = cloud_storage_account::parse(
            U("UseDevelopmentStorage=true"));
      auto blob_client = storage_account.create_cloud_blob_client();
      auto container = blob_client.get_container_reference(
            U("textdata"));
      bool created = container.create_if_not_exists();
    
      continuation_token token;
    
      auto result = container.list_blobs_segmented(token);
    
      for (auto dir : result.directories())
      {
        ucout << U("Directory: ") << dir.uri().path() << endl;
        ucout << endl;
    
        continuation_token dir_token;
        auto resultInner = dir.list_blobs_segmented(dir_token);
    
        for (auto item : resultInner.blobs())
        {
          ucout << item.name() << endl;
          ucout << item.uri().path() << endl;
          ucout << item.properties().content_type() << endl;
          ucout << endl;
        }   
      }
    

    下载blob

    void DownloadBlobData()
    {
        auto storage_account = cloud_storage_account::parse(U("UseDevelopmentStorage=true"));
        auto blob_client = storage_account.create_cloud_blob_client();
        auto container = blob_client.get_container_reference(U("textdata"));
        bool created = container.create_if_not_exists();
    
        // Read the text content directly
        auto text_blob1 = container.get_block_blob_reference(U("texts/text1"));
        auto text = text_blob1.download_text();
        ucout << text << endl;
    
        // Download the blob data to a file
        auto text_blob2 = container.get_block_blob_reference(U("texts/text2"));
        text_blob2.download_to_file(U("d:\\tmp\\blobdata.txt"));
    
        // Download the blob data to an ostream
        stringstreambuf buffer;
        concurrency::streams::ostream output(buffer);
        text_blob2.download_to_stream(output);
        cout << buffer.collection() << endl;
    }

    希望能帮到你。

    谢谢


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年11月3日 6:12
    版主