Batch upload document to Azure Cosmos DB using Perl

AzureGuru 40 Reputation points
2024-06-21T19:02:48.8033333+00:00

I have successfully set up Postman Collections to access Microsoft Azure Cosmos DB via REST APIs by following https://github.com/sajeetharan/CosmosDBPostmanGuide. The next step I need to perform is gather sample JSON data to be batch uploaded to our Azure Cosmos DB account using Perl. In my Postman setup, I used Pre-script which is written in Javascript. I don't know if the same thing can be accomplished using Perl. Has anyone successfully uploaded JSON document to Azure Cosmos DB using Perl?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,539 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AzureGuru 40 Reputation points
    2024-06-25T13:21:56.48+00:00

    Yes, I checked those. I'm not familiar with the JSON tool for bulk uploading, and we don't use .NET language. I'll try the JSON GUI tool, but since we usually create Unix cron job to handle batch processing automatically, I still think using Perl.Yesterday I used ChatGPT to help generate Perl code for Auth Token. Below is the code generated and I slightly modified the variable names. I tested the code and got output similar to Javascript code. For example, my output looks like:

    Current UTC Date: Tue, 25 Jun 2024 13:16:23 GMT
    x-ms-date: Tue, 25 Jun 2024 13:16:23 GMT
    token: type=master&ver=1.0&sig=v4flGat%2Bx%2BxAxv%2FpGSxYS3NxY%2Fw%3D
    

    Is it possible to replace Pre-script in Postman using below Perl code to test if it works?

    
    #/usr/bin/env perl
    
    use strict;
    use warnings;
    
    use Time::Piece;
    use MIME::Base64;
    use Digest::HMAC_SHA1;
    use URI::Escape;
    
    my $date = gmtime->strftime('%a, %d %b %Y %H:%M:%S GMT');
    my $token = get_authorization_token('GET', 'dbs', '', $date);
    
    print "Current UTC Date: $date\n";
    print "x-ms-date: $date\ntoken: $token\n";
    
    # Function to generate authorization token 
    sub get_authorization_token {
        my ($method, $resource_type, $resource_link, $date) = @_;
        my $text = join("\n", $method, $resource_type, $resource_link, $date, "", "");
        $text = lc($text);
        my $master_key = $ENV{COSMOS_KEY};
        my $key = decode_base64($master_key);
        my $hmac = Digest::HMAC_SHA1->new($key);
        $hmac->add($text);
        my $signature = encode_base64($hmac->digest, "");
        my $auth_token = "type=master&ver=1.0&sig=" . uri_escape($signature);
    
        return $auth_token;
    }
    
    0 comments No comments