in dlp/api/Snippets/DeidentifyWithDateShift.cs [26:123]
public static DeidentifyContentResponse Deidentify(
string projectId,
string inputCsvFilePath,
int lowerBoundDays,
int upperBoundDays,
string dateFields,
string contextField,
string keyName,
string wrappedKey)
{
var hasKeyName = !string.IsNullOrEmpty(keyName);
var hasWrappedKey = !string.IsNullOrEmpty(wrappedKey);
var hasContext = !string.IsNullOrEmpty(contextField);
bool allFieldsSet = hasKeyName && hasWrappedKey && hasContext;
bool noFieldsSet = !hasKeyName && !hasWrappedKey && !hasContext;
if (!(allFieldsSet || noFieldsSet))
{
throw new ArgumentException("Must specify ALL or NONE of: {contextFieldId, keyName, wrappedKey}!");
}
var dlp = DlpServiceClient.Create();
// Read file
var csvLines = File.ReadAllLines(inputCsvFilePath);
var csvHeaders = csvLines[0].Split(',');
var csvRows = csvLines.Skip(1).ToArray();
// Convert dates to protobuf format, and everything else to a string
var protoHeaders = csvHeaders.Select(header => new FieldId { Name = header });
var protoRows = csvRows.Select(csvRow =>
{
var rowValues = csvRow.Split(',');
var protoValues = rowValues.Select(rowValue =>
System.DateTime.TryParse(rowValue, out var parsedDate)
? new Value { DateValue = Google.Type.Date.FromDateTime(parsedDate) }
: new Value { StringValue = rowValue });
var rowObject = new Table.Types.Row();
rowObject.Values.Add(protoValues);
return rowObject;
});
var dateFieldList = dateFields
.Split(',')
.Select(field => new FieldId { Name = field });
// Construct + execute the request
var dateShiftConfig = new DateShiftConfig
{
LowerBoundDays = lowerBoundDays,
UpperBoundDays = upperBoundDays
};
dateShiftConfig.Context = new FieldId { Name = contextField };
dateShiftConfig.CryptoKey = new CryptoKey
{
KmsWrapped = new KmsWrappedCryptoKey
{
WrappedKey = ByteString.FromBase64(wrappedKey),
CryptoKeyName = keyName
}
};
var deidConfig = new DeidentifyConfig
{
RecordTransformations = new RecordTransformations
{
FieldTransformations =
{
new FieldTransformation
{
PrimitiveTransformation = new PrimitiveTransformation
{
DateShiftConfig = dateShiftConfig
},
Fields = { dateFieldList }
}
}
}
};
var response = dlp.DeidentifyContent(
new DeidentifyContentRequest
{
Parent = new LocationName(projectId, "global").ToString(),
DeidentifyConfig = deidConfig,
Item = new ContentItem
{
Table = new Table
{
Headers = { protoHeaders },
Rows = { protoRows }
}
}
});
return response;
}