Exemplo n.º 1
0
        /// <summary>
        /// Attempts to retrieve a blob path out of log entry's RequestedObjectKey.
        /// </summary>
        /// <returns>
        /// A valid instance of <see cref="BlobPath"/>, or null if log entry is not associated with a blob.
        /// </returns>
        /// <exception cref="FormatException">If fails to determine blob path components, i.e. account, container name,
        /// and blob name.</exception>
        public BlobPath ToBlobPath()
        {
            if (!ServiceType.HasValue || ServiceType != StorageServiceType.Blob)
            {
                return(null);
            }

            string path;

            Uri keyAsUri = new Uri(RequestedObjectKey, UriKind.RelativeOrAbsolute);

            if (keyAsUri.IsAbsoluteUri)
            {
                // assuming key is "https://storagesample.blob.core.windows.net/sample-container/sample-blob.txt"
                path = keyAsUri.Segments.Length > 1 ? keyAsUri.LocalPath.Substring(1) : String.Empty;
            }
            else
            {
                // assuming key is "/account/container/blob"
                int startPos = RequestedObjectKey.Length > 1 ? RequestedObjectKey.IndexOf('/', 1) : -1;
                path = startPos != -1 ? RequestedObjectKey.Substring(startPos + 1) : String.Empty;
            }

            if (String.IsNullOrEmpty(path))
            {
                throw new FormatException("Failed to parse RequestedObjectKey property of the log entry. " +
                                          "It should be in one of the supported formats: " +
                                          @"""https://account.blob.core.windows.net/container/blob"", or" +
                                          @"""/account/container/blob""");
            }

            BlobPath blobPath;

            if (!BlobPath.TryParse(path, false, out blobPath))
            {
                throw new FormatException("Failed to parse RequestedObjectKey property of the log entry. " +
                                          "Blob identifiers must be in the format container/blob.");
            }

            return(blobPath);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempts to retrieve a blob path out of log entry's RequestedObjectKey.
        /// </summary>
        /// <returns>
        /// A valid instance of <see cref="BlobPath"/>, or null if log entry is not associated with a blob.
        /// </returns>
        public BlobPath ToBlobPath()
        {
            if (!ServiceType.HasValue || ServiceType != StorageServiceType.Blob)
            {
                return(null);
            }

            string path;

            Uri keyAsUri = new Uri(RequestedObjectKey, UriKind.RelativeOrAbsolute);

            if (keyAsUri.IsAbsoluteUri)
            {
                // assuming key is "https://storagesample.blob.core.windows.net/sample-container/sample-blob.txt"
                path = keyAsUri.Segments.Length > 1 ? keyAsUri.LocalPath.Substring(1) : String.Empty;
            }
            else
            {
                // assuming key is "/account/container/blob"
                int startPos = RequestedObjectKey.Length > 1 ? RequestedObjectKey.IndexOf('/', 1) : -1;
                path = startPos != -1 ? RequestedObjectKey.Substring(startPos + 1) : String.Empty;
            }

            if (String.IsNullOrEmpty(path))
            {
                return(null);
            }

            BlobPath blobPath;

            if (!BlobPath.TryParse(path, false, out blobPath))
            {
                return(null);
            }

            return(blobPath);
        }