Update
I am trying to move data from cosmos using c# .The code is reading all data from cosmos (approx 70k). I am looking to copy to blob and delete from cosmos. But I am getting error:
Error :Entity with the specified id does not exist in the system.
but I can see it is there as I am using same Document after reading as shown in code.
I also checked How to delete object from Cosmos DB without knowing the partition key value?
Though I can see in cosmos that I have a partion key "/content.provider.id" . How can I use this partition key to delete .
foreach (Document document in item)
{
var content = document.ToString();
var json = JsonConvert.DeserializeObject(content);
string blobId = String.Concat(document.Id);
using (Stream ms = new MemoryStream(Encoding.UTF8.GetBytes(json.ToString().ToCharArray())))
{
try
{
await (ConnectionFactory.GetConnectionBlob(DestinationStorageContainerName, storageConnectionString)).GetBlobClient(blobId).UploadAsync(ms, true);
await (ConnectionFactory.GetConnectionDocumentClient(cosmosAccount, cosmosKey)).DeleteDocumentAsync(
UriFactory.CreateDocumentUri(SourceCosmosDatabaseName, SourceCosmosCollectionName, document.Id),
new RequestOptions() { PartitionKey = new PartitionKey(Undefined.Value) });
Console.WriteLine("All files copied to Storage - " + count);
count++;
}
catch (CosmosException ce)
{
Console.WriteLine(ce.Message);
Console.WriteLine("data Coped" + count);
}
}
}
I am looking to copy to blob and delete from cosmos.
Below are the steps I followed to delete from Cosmos and store it in Blob:
Using the supplied Cosmos DB configuration, CosmosClient
creates a Cosmos DB client.
Uses the ReadItemAsync
function to read a document from Cosmos DB.
The document is identified by its partition key and cosmosDbItemIdToDelete
.
The value of the partition key is passed as an input to the PartitionKey
constructor. I used the document's Id as the partition key value in the code.
Use the partition key to identify the correct partition where the document resides. This ensures that the document is deleted from the correct partition.
cosmosDbItemIdToDelete
is used as the partition key value to locate and delete the document within the appropriate partition.
The BlobServiceClient
and BlobContainerClient
for Blob Storage are created using the Azure Blob Storage SDK and the supplied configuration.
The data from the Cosmos DB document is serialized into the necessary format and uploaded to Azure Blob Storage with the given blobFileName
.
Documents copied from Cosmos DB to Blob Storage are deleted using the DeleteItemAsync()
method.
Below Is the code I tried with:
static async Task Main()
{
string cosmosDbEndpoint = "*****";
string cosmosDbKey = "*****";
string cosmosDbDatabaseName = "newDb";
string cosmosDbContainerName = "newCont";
string cosmosDbItemIdToDelete = "1";
string blobStorageConnectionString = "*****";
string blobContainerName = "balaji";
string blobFileName = "balajiDoc.txt";
Console.WriteLine("Starting the data transfer and deletion process...");
try
{
using CosmosClient cosmosClient = new CosmosClient(cosmosDbEndpoint, cosmosDbKey);
Database cosmosDatabase = cosmosClient.GetDatabase(cosmosDbDatabaseName);
Microsoft.Azure.Cosmos.Container cosmosContainer = cosmosDatabase.GetContainer(cosmosDbContainerName);
ItemResponse<MyDocument> cosmosResponse = await cosmosContainer.ReadItemAsync<MyDocument>(
cosmosDbItemIdToDelete,
new PartitionKey(cosmosDbItemIdToDelete));
Console.WriteLine("Document read from Cosmos DB.");
BlobServiceClient blobServiceClient = new BlobServiceClient(blobStorageConnectionString);
BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient(blobContainerName);
BlobClient blobClient = blobContainerClient.GetBlobClient(blobFileName);
using Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(cosmosResponse.Resource)));
await blobClient.UploadAsync(stream, true);
Console.WriteLine("Document data copied to Azure Blob Storage.");
await cosmosContainer.DeleteItemAsync<MyDocument>(
cosmosDbItemIdToDelete,
new PartitionKey(cosmosDbItemIdToDelete));
Console.WriteLine("Document deleted from Cosmos DB.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
Output:
In Console:
Starting the data transfer and deletion process...
Document read from Cosmos DB.
Document data copied to Azure Blob Storage.
Document deleted from Cosmos DB.
In Blob:
{"Id":"1","Name":"Pavan","Age":25}