@@ -5,12 +5,18 @@ import models = require('artifact-engine/Models');
5
5
import tl = require( 'vsts-task-lib/task' ) ;
6
6
7
7
export class AzureBlobProvider implements models . IArtifactProvider {
8
- constructor ( storageAccount : string , container : string , accessKey : string , prefixFolderPath ?: string , host ?: string ) {
8
+
9
+ constructor ( storageAccount : string , container : string , accessKey : string , prefixFolderPath ?: string , host ?: string , addPrefixToDownloadedItems ?: boolean ) {
9
10
this . _storageAccount = storageAccount ;
10
11
this . _accessKey = accessKey ;
11
12
this . _container = container ;
12
- this . _prefixFolderPath = prefixFolderPath ;
13
+ if ( ! ! prefixFolderPath ) {
14
+ this . _prefixFolderPath = prefixFolderPath . endsWith ( "/" ) ? prefixFolderPath : prefixFolderPath + "/" ;
15
+ } else {
16
+ this . _prefixFolderPath = "" ;
17
+ }
13
18
this . _blobSvc = azureStorage . createBlobService ( this . _storageAccount , this . _accessKey , host ) ;
19
+ this . _addPrefixToDownloadedItems = ! ! addPrefixToDownloadedItems ;
14
20
}
15
21
16
22
public putArtifactItem ( item : models . ArtifactItem , readStream : NodeJS . ReadableStream ) : Promise < models . ArtifactItem > {
@@ -52,12 +58,18 @@ export class AzureBlobProvider implements models.IArtifactProvider {
52
58
}
53
59
54
60
public getArtifactItems ( artifactItem : models . ArtifactItem ) : Promise < models . ArtifactItem [ ] > {
55
- return this . _getItems ( this . _container , artifactItem . path ) ;
61
+ throw new Error ( tl . loc ( "GetArtifactItemsNotSupported" ) ) ;
56
62
}
57
63
58
64
public getArtifactItem ( artifactItem : models . ArtifactItem ) : Promise < NodeJS . ReadableStream > {
59
65
return new Promise ( ( resolve , reject ) => {
60
- var readStream : NodeJS . ReadableStream = this . _blobSvc . createReadStream ( this . _container , artifactItem . path , null ) ;
66
+ var readStream : NodeJS . ReadableStream ;
67
+ if ( ! this . _addPrefixToDownloadedItems && ! ! this . _prefixFolderPath ) {
68
+ // Adding prefix path to get the absolute path
69
+ readStream = this . _blobSvc . createReadStream ( this . _container , this . _prefixFolderPath + artifactItem . path , null ) ;
70
+ } else {
71
+ readStream = this . _blobSvc . createReadStream ( this . _container , artifactItem . path , null ) ;
72
+ }
61
73
resolve ( readStream ) ;
62
74
} ) ;
63
75
}
@@ -83,20 +95,34 @@ export class AzureBlobProvider implements models.IArtifactProvider {
83
95
}
84
96
85
97
private _getItems ( container : string , parentRelativePath ?: string ) : Promise < models . ArtifactItem [ ] > {
86
- var promise = new Promise < models . ArtifactItem [ ] > ( ( resolve , reject ) => {
98
+ var promise = new Promise < models . ArtifactItem [ ] > ( async ( resolve , reject ) => {
87
99
var items : models . ArtifactItem [ ] = [ ] ;
100
+ var continuationToken = null ;
101
+ var result ;
102
+ do {
103
+ result = await this . _getListOfItemsInsideContainer ( container , parentRelativePath , continuationToken ) ;
104
+ items = items . concat ( this . _convertBlobResultToArtifactItem ( result . entries ) ) ;
105
+ continuationToken = result . continuationToken ;
106
+ if ( ! ! continuationToken ) {
107
+ console . log ( tl . loc ( "ContinuationTokenExistsFetchingRemainingFiles" ) ) ;
108
+ }
109
+ } while ( continuationToken ) ;
110
+
111
+ console . log ( tl . loc ( "SuccessFullyFetchedItemList" ) ) ;
112
+ resolve ( items ) ;
113
+ } ) ;
88
114
89
- this . _blobSvc . listBlobsSegmentedWithPrefix ( container , parentRelativePath , null , ( error , result ) => {
115
+ return promise ;
116
+ }
117
+
118
+ private async _getListOfItemsInsideContainer ( container , parentRelativePath , continuationToken ) : Promise < azureStorage . BlobService . ListBlobsResult > {
119
+ var promise = new Promise < azureStorage . BlobService . ListBlobsResult > ( ( resolve , reject ) => {
120
+ this . _blobSvc . listBlobsSegmentedWithPrefix ( container , parentRelativePath , continuationToken , async ( error , result ) => {
90
121
if ( ! ! error ) {
91
122
console . log ( tl . loc ( "FailedToListItemInsideContainer" , container , error . message ) ) ;
92
123
reject ( error ) ;
93
124
} else {
94
- console . log ( tl . loc ( "SuccessFullyFetchedItemList" ) ) ;
95
- if ( result . continuationToken ) {
96
- tl . warning ( tl . loc ( "ArtifactItemsTruncationWarning" ) ) ;
97
- }
98
- items = this . _convertBlobResultToArtifactItem ( result . entries ) ;
99
- resolve ( items ) ;
125
+ resolve ( result ) ;
100
126
}
101
127
} ) ;
102
128
} ) ;
@@ -111,7 +137,12 @@ export class AzureBlobProvider implements models.IArtifactProvider {
111
137
artifactitem . itemType = models . ItemType . File ;
112
138
artifactitem . fileLength = parseInt ( element . contentLength ) ;
113
139
artifactitem . lastModified = new Date ( element . lastModified + 'Z' ) ;
114
- artifactitem . path = element . name ;
140
+ if ( ! this . _addPrefixToDownloadedItems && ! ! this . _prefixFolderPath ) {
141
+ // Supplying relative path without prefix; removing the first occurence
142
+ artifactitem . path = element . name . replace ( this . _prefixFolderPath , "" ) . trim ( ) ;
143
+ } else {
144
+ artifactitem . path = element . name ;
145
+ }
115
146
artifactItems . push ( artifactitem ) ;
116
147
} ) ;
117
148
@@ -124,4 +155,5 @@ export class AzureBlobProvider implements models.IArtifactProvider {
124
155
private _prefixFolderPath : string ;
125
156
private _isContainerExists : boolean = false ;
126
157
private _blobSvc : azureStorage . BlobService ;
158
+ private _addPrefixToDownloadedItems : boolean = false ;
127
159
}
0 commit comments