Search code examples
azure-storageazure-data-factorycustom-activity

Azure Data Factory get blob path with sas token in custom activity


I'm trying to build a custom activity in Azure Data Factory that gets a blob as input dataset and would like to pass this blob's sas token path to an API that requires this type of path.

Is there any way to get the blob's path with the sas token in the custom activity?


Solution

  • I figured out a way to do it. Part of the custom activity in ADF v1 is the Execute method that has a context parameter. From that context you can get the connection string to the blob storage and the path of the blob and then you can extract the sas token like this:

    public override IDictionary<string, string> Execute(
    AOMDotNetActivityContext context,
    IActivityLogger logger)
    {
        string blobConnectionString = context.ConnectionString;
        CloudStorageAccount inputStorageAccount = CloudStorageAccount.Parse(blobConnectionString);
        var blob = new CloudBlob(new Uri(inputStorageAccount.BlobEndpoint, Path.Combine(context.FolderPath, context.FileName)), inputStorageAccount.Credentials);
        SharedAccessBlobPolicy adHocSAS = new SharedAccessBlobPolicy()
        {
            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(48),
            Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Delete
        };
        string sasBlobToken = blob.GetSharedAccessSignature(adHocSAS);
        string fullUri = new Uri(blob.Uri, sasBlobToken).ToString();