Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active April 20, 2021 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/f67cb1e276e5d5dc9a16786cabda0d40 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/f67cb1e276e5d5dc9a16786cabda0d40 to your computer and use it in GitHub Desktop.
Add, Edit, Extract and Remove Metadata from Images programmatically using a REST API in C#.
Add, Edit, Extract and Remove Metadata from Images
1. Programmatically upload a JPEG image file on the cloud
2. Add Metadata to JPEG Image programmatically on the cloud using a REST API in C#.
3. Extract Metadata of JPEG Image programmatically on the cloud using a REST API in C#.
4. Edit Metadata of JPEG Image programmatically on the cloud using a REST API in C#.
5. Remove Metadata of JPEG Image programmatically on the cloud using a REST API in C#.
// api initialization
var apiInstance = new MetadataApi(configuration);
// input image path
var fileInfo = new GroupDocs.Metadata.Cloud.Sdk.Model.FileInfo
{
FilePath = "input.jpg"
};
// define metadata add options
var options = new AddOptions
{
FileInfo = fileInfo,
Properties = new List<AddProperty>
{
new AddProperty
{
Value = "All rights reserved.",
Type = "String",
SearchCriteria = new SearchCriteria
{
TagOptions = new TagOptions
{
PossibleName = "Copyright"
}
},
}
}
};
// add request
var request = new AddRequest(options);
var response = apiInstance.Add(request);
string clientID = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
string clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
string myStorage = "";
Configuration configuration = new Configuration(clientID, clientSecret);
configuration.ApiBaseUrl = "https://api.groupdocs.cloud";
// api initialization
var fileApi = new FileApi(configuration);
// default path
var file = "metadata\\add_metadata\\input_jpg\\input.jpg";
// download request
var downloadRequest = new DownloadFileRequest(file, myStorage);
Stream downloadResponse = fileApi.DownloadFile(downloadRequest);
using (var fileStream = File.Create("C:\\Files\\input.jpg"))
{
downloadResponse.Seek(0, SeekOrigin.Begin);
downloadResponse.CopyTo(fileStream);
}
// api initialization
var apiInstance = new MetadataApi(configuration);
// input image path
var fileInfo = new GroupDocs.Metadata.Cloud.Sdk.Model.FileInfo
{
FilePath = "input.jpg"
};
// define metadata set options
var options = new SetOptions
{
FileInfo = fileInfo,
Properties = new List<SetProperty>
{
new SetProperty
{
NewValue = "Copyright 2021",
SearchCriteria = new SearchCriteria
{
TagOptions = new TagOptions
{
PossibleName = "Copyright"
}
},
Type = "String"
}
}
};
// update request
var request = new SetRequest(options);
var response = apiInstance.Set(request);
// api initialization
var apiInstance = new MetadataApi(configuration);
// source file
var fileInfo = new GroupDocs.Metadata.Cloud.Sdk.Model.FileInfo
{
FilePath = "input.jpg"
};
// extract optioins
var options = new ExtractOptions
{
FileInfo = fileInfo
};
// extract request
var request = new ExtractRequest(options);
var response = apiInstance.Extract(request);
// extracted metadata
if (response.MetadataTree.InnerPackages != null)
{
var innerPackages = response.MetadataTree.InnerPackages;
for (int i = 0; i < innerPackages.Count; i++)
{
Console.WriteLine($"\nPackage: {innerPackages[i].PackageName}");
var packageProperties = innerPackages[i].PackageProperties;
for (int j = 0; j < packageProperties.Count; j++)
{
Console.WriteLine(packageProperties[j].Name + " : " + packageProperties[j].Value);
}
}
}
// api initialization
var apiInstance = new MetadataApi(configuration);
// input image path
var fileInfo = new GroupDocs.Metadata.Cloud.Sdk.Model.FileInfo
{
FilePath = "input.jpg"
};
// define metadata remove options
var options = new RemoveOptions
{
FileInfo = fileInfo,
SearchCriteria = new SearchCriteria
{
TagOptions = new TagOptions
{
PossibleName = "Copyright"
}
}
};
// remove request
var request = new RemoveRequest(options);
var response = apiInstance.Remove(request);
// api initialization
FileApi fileApi = new FileApi(configuration);
string path = @"C:\Files";
var file = Directory.GetFiles(path, "input.jpg", SearchOption.AllDirectories).FirstOrDefault();
if (file.Length != 0)
{
var relativeFilePath = file.Replace(path, string.Empty).Trim(Path.DirectorySeparatorChar);
var fileStream = File.Open(file, FileMode.Open);
fileApi.UploadFile(new UploadFileRequest(relativeFilePath, fileStream, myStorage));
fileStream.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment