void blob_service_test_base_with_objects_to_delete::create_blobs(const azure::storage::cloud_blob_container& container, const utility::string_t& prefix, std::size_t num)
{
    for (std::size_t i = 0; i < num; i++)
    {
        auto index = utility::conversions::print_string(i);
        auto blob = container.get_block_blob_reference(prefix + index);
        m_blobs_to_delete.push_back(blob);
        blob.upload_text(U("test"), azure::storage::access_condition(), azure::storage::blob_request_options(), m_context);
    }
}
Ejemplo n.º 2
0
// Note that, blob name is case sensitive.
void download_block_blob(azure::storage::cloud_blob_container &container,
    const utility::string_t &blob_name, const utility::string_t &target_file_name, 
    utility::size64_t downloading_bytes_each_time)
{
    auto blob = container.get_block_blob_reference(blob_name);
    if (!blob.exists()) {
        ucerr << U("Block blob \"") << blob_name << U("\" doesn't exist.\n");
        return;
    }

    boost::filesystem::path target_path(target_file_name);
    if (boost::filesystem::exists(target_path))
    {
        ucout << U("Warning! File \"") << target_file_name 
            << U("\" already exists, and it will be overwrite.") << std::endl;
    }
    else
    {
        target_path.remove_filename();
        if (!boost::filesystem::exists(target_path))
        {
            ucout << U("Creating directory \"") << target_path.string<utility::string_t>()
                << U("\"...") << std::endl;
            if (!boost::filesystem::create_directories(target_path))
            {
                ucerr << U("Failed to create directory \"")
                    << target_path.string<utility::string_t>() << U("\"!\n");
                return;
            }
        }
    }

    // Get blob's size(in bytes)
    auto blob_size = blob.properties().size();

    if (0 == blob_size) {
        blob.download_to_file(target_file_name);
        return;
    }

    assert(downloading_bytes_each_time > 0);
    if (downloading_bytes_each_time == 0 || downloading_bytes_each_time > blob_size) {
        downloading_bytes_each_time = blob_size;
    }

    std::ofstream out_file(target_file_name, std::ofstream::binary);
    if (!out_file) {
        ucout << U("Opening file \"") << target_file_name << U("\" failed.\n");
        return;
    }

    ucout << U("Downloading file to: ") << target_file_name << U("...");
    boost::progress_display prog(blob_size);

    utility::size64_t offset = 0;
    utility::size64_t bytes_read;
    while (offset < blob_size) {
        // Create a memory buffer
        concurrency::streams::container_buffer<std::vector<uint8_t>> buffer;
        // Read data
        concurrency::streams::ostream tmp_out_stream(buffer);
        blob.download_range_to_stream(tmp_out_stream, offset, downloading_bytes_each_time);
        bytes_read = buffer.collection().size();
        // Write out...
        out_file.write(reinterpret_cast<char *>(buffer.collection().data()), bytes_read);
        //out_file.flush();
        // Upgrade the progress bar and other states
        prog += bytes_read;
        offset += bytes_read;
    }

    // The last step...
    blob.download_block_list();
}