C# Code Sample Templates for cloud.google.com
Last active
February 26, 2019 05:56
-
-
Save beccasaurus/316e630541bc53d07da761b38fe473d8 to your computer and use it in GitHub Desktop.
C# Code Sample Templates for cloud.google.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [START dlp_inspect_file] | |
// Imports the Google Cloud Data Loss Prevention library | |
using Google.Cloud.Dlp.V2; | |
using Google.Api.Gax.ResourceNames; | |
// Imports other required libraries | |
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
public class InspectStringSample | |
{ | |
/// <summary> | |
/// Inspects the provided file for sensitive data. | |
///</summary> | |
/// <param name="projectId">Your Google Cloud Project ID.</param> | |
/// <param name="filepath">The path to the specified file to inspect.</param> | |
/// <param name="mimeType">The mime type of the specifed file.</param> | |
public static IEnumerable<Finding> InspectString( | |
string projectId = "YOUR_PROJECT_ID", | |
string filepath = "path/to/image.png", | |
string mimeType = "image/png" | |
) | |
{ | |
// Instantiates a client | |
DlpServiceClient dlp = DlpServiceClient.Create(); | |
// Get the bytes of the file | |
var filedata = ByteString.FromStream(new FileStream(filepath, FileMode.Open)); | |
// Construct request | |
var request = new InspectContentRequest | |
{ | |
ParentAsProjectName = new ProjectName(projectId), | |
Item = new ContentItem | |
{ | |
Data = filedata, | |
Type = mimeType | |
}, | |
InspectConfig = new InspectConfig | |
{ | |
// The info types of information to match | |
InfoTypes = { | |
new InfoType { Name = "PHONE_NUMBER" }, | |
new InfoType { Name = "EMAIL_ADDRESS" }, | |
new InfoType { Name = "CREDIT_CARD_NUMBER" } | |
}, | |
// The minimum likelihood before returning a match | |
MinLikelihood = Likelihood.Unspecified, | |
// Whether to include the matching string | |
IncludeQuote = true, | |
Limits = new InspectConfig.Types.FindingLimits | |
{ | |
// The maximum number of findings to report per request | |
// (0 = server maximum) | |
MaxFindingsPerRequest = 0 | |
} | |
} | |
}; | |
// Execute request | |
InspectContentResponse response = dlp.InspectContent(request); | |
// Inspect response | |
var findings = response.Result.Findings; | |
if (findings.Count > 0) | |
{ | |
Console.WriteLine("Findings:"); | |
foreach (var finding in findings) | |
{ | |
if (includeQuote) | |
{ | |
Console.WriteLine($"Quote: {finding.Quote}"); | |
} | |
Console.WriteLine($"InfoType: {finding.InfoType}"); | |
Console.WriteLine($"Likelihood: {finding.Likelihood}"); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("No findings."); | |
} | |
// Return findings | |
return findings; | |
} | |
} | |
// [END dlp_inspect_file] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [START dlp_inspect_string] | |
// Imports the Google Cloud Data Loss Prevention library | |
using Google.Cloud.Dlp.V2; | |
using Google.Api.Gax.ResourceNames; | |
// Imports other required libraries | |
using System; | |
using System.Collections.Generic; | |
public class InspectStringSample | |
{ | |
/// <summary> | |
/// Inspects the provided text for sensitive data. | |
///</summary> | |
/// <param name="projectId">Your Google Cloud Project ID.</param> | |
/// <param name="textToInspect">The text to inspect.</param> | |
public static IEnumerable<Finding> InspectString( | |
string projectId = "YOUR_PROJECT_ID", | |
string textToInspect = "My name is Gary and my email is gary@example.com" | |
) | |
{ | |
// Instantiates a client | |
DlpServiceClient dlp = DlpServiceClient.Create(); | |
// Construct request | |
var request = new InspectContentRequest | |
{ | |
ParentAsProjectName = new ProjectName(projectId), | |
Item = new ContentItem | |
{ | |
Value = textToInspect | |
}, | |
InspectConfig = new InspectConfig | |
{ | |
// The info types of information to match | |
InfoTypes = { | |
new InfoType { Name = "PHONE_NUMBER" }, | |
new InfoType { Name = "EMAIL_ADDRESS" }, | |
new InfoType { Name = "CREDIT_CARD_NUMBER" } | |
}, | |
// The minimum likelihood before returning a match | |
MinLikelihood = Likelihood.Unspecified, | |
// Whether to include the matching string | |
IncludeQuote = true, | |
Limits = new InspectConfig.Types.FindingLimits | |
{ | |
// The maximum number of findings to report per request | |
// (0 = server maximum) | |
MaxFindingsPerRequest = 0 | |
} | |
} | |
}; | |
// Execute request | |
InspectContentResponse response = dlp.InspectContent(request); | |
// Inspect response | |
var findings = response.Result.Findings; | |
if (findings.Count > 0) | |
{ | |
Console.WriteLine("Findings:"); | |
foreach (var finding in findings) | |
{ | |
if (includeQuote) | |
{ | |
Console.WriteLine($"Quote: {finding.Quote}"); | |
} | |
Console.WriteLine($"InfoType: {finding.InfoType}"); | |
Console.WriteLine($"Likelihood: {finding.Likelihood}"); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("No findings."); | |
} | |
// Return findings | |
return findings; | |
} | |
} | |
// [END dlp_inspect_string] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment