public async Task ReadCloudStorageData()

in processing-pipelines/common/csharp/CloudEventReader.cs [37:101]


        public async Task<(string, string)> ReadCloudStorageData(HttpContext context)
        {
            _logger.LogInformation("Reading cloud storage data");

            string bucket = null, name = null;
            CloudEvent cloudEvent;
            CloudEventFormatter formatter;
            var ceType = context.Request.Headers["ce-type"];

            switch (ceType)
            {
                case LogEntryData.WrittenCloudEventType:
                    //"protoPayload" : {"resourceName":"projects/_/buckets/events-atamel-images-input/objects/atamel.jpg}";
                    formatter = CloudEventFormatterAttribute.CreateFormatter(typeof(LogEntryData));
                    cloudEvent = await context.Request.ToCloudEventAsync(formatter);
                    _logger.LogInformation($"Received CloudEvent\n{cloudEvent.GetLog()}");

                    var logEntryData = (LogEntryData)cloudEvent.Data;
                    var tokens = logEntryData.ProtoPayload.ResourceName.Split('/');
                    bucket = tokens[3];
                    name = tokens[5];
                    break;
                case StorageObjectData.FinalizedCloudEventType:
                    formatter = CloudEventFormatterAttribute.CreateFormatter(typeof(StorageObjectData));
                    cloudEvent = await context.Request.ToCloudEventAsync(formatter);
                    _logger.LogInformation($"Received CloudEvent\n{cloudEvent.GetLog()}");

                    var storageObjectData = (StorageObjectData)cloudEvent.Data;
                    bucket = storageObjectData.Bucket;
                    name = storageObjectData.Name;
                    break;
                case MessagePublishedData.MessagePublishedCloudEventType:
                    // {"message": {
                    //     "data": "eyJidWNrZXQiOiJldmVudHMtYXRhbWVsLWltYWdlcy1pbnB1dCIsIm5hbWUiOiJiZWFjaC5qcGcifQ==",
                    // },"subscription": "projects/events-atamel/subscriptions/cre-europe-west1-trigger-resizer-sub-000"}
                    formatter = CloudEventFormatterAttribute.CreateFormatter(typeof(MessagePublishedData));
                    cloudEvent = await context.Request.ToCloudEventAsync(formatter);
                    _logger.LogInformation($"Received CloudEvent\n{cloudEvent.GetLog()}");

                    var messagePublishedData = (MessagePublishedData)cloudEvent.Data;
                    var pubSubMessage = messagePublishedData.Message;
                    _logger.LogInformation($"Type: {ceType} data: {pubSubMessage.Data.ToBase64()}");

                    var decoded = pubSubMessage.Data.ToStringUtf8();
                    _logger.LogInformation($"decoded: {decoded}");

                    var parsed = JValue.Parse(decoded);
                    bucket = (string)parsed["bucket"];
                    name = (string)parsed["name"];
                    break;
                default:
                    // Data:
                    // {"bucket":"knative-atamel-images-input","name":"beach.jpg"}
                    formatter = new JsonEventFormatter();
                    cloudEvent = await context.Request.ToCloudEventAsync(formatter);
                    _logger.LogInformation($"Received CloudEvent\n{cloudEvent.GetLog()}");

                    dynamic data = cloudEvent.Data;
                    bucket = data.bucket;
                    name = data.name;
                    break;
            }
            _logger.LogInformation($"Extracted bucket: {bucket} and name: {name}");
            return (bucket, name);
        }