@@ -545,10 +545,25 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
545545 } else {
546546 const types = await this . queryColumnTypes ( options . query . sql , options . query . params ) ;
547547 const connection = await this . getConnection ( ) ;
548- const { bucketType, bucketName } =
548+ const { bucketType } =
549549 < SnowflakeDriverExportBucket > this . config . exportBucket ;
550+
551+ let bucketName : string ;
552+ let exportPrefix : string ;
553+ let path : string ;
554+
555+ if ( bucketType === 'azure' ) {
556+ ( { bucketName, path } = this . parseBucketUrl ( this . config . exportBucket ! . bucketName ) ) ;
557+ const pathArr = path . split ( '/' ) ;
558+ bucketName = `${ bucketName } /${ pathArr [ 0 ] } ` ;
559+ exportPrefix = pathArr . length > 1 ? `${ pathArr . slice ( 1 ) . join ( '/' ) } /${ tableName } ` : tableName ;
560+ } else {
561+ ( { bucketName, path } = this . parseBucketUrl ( this . config . exportBucket ! . bucketName ) ) ;
562+ exportPrefix = path ? `${ path } /${ tableName } ` : tableName ;
563+ }
564+
550565 const unloadSql = `
551- COPY INTO '${ bucketType } ://${ bucketName } /${ tableName } /'
566+ COPY INTO '${ bucketType } ://${ bucketName } /${ exportPrefix } /'
552567 FROM (${ options . query . sql } )
553568 ${ this . exportOptionsClause ( options ) } ` ;
554569 const result = await this . execute < UnloadResponse [ ] > (
@@ -594,10 +609,25 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
594609 ) : Promise < TableStructure > {
595610 const types = await this . tableColumnTypes ( tableName ) ;
596611 const connection = await this . getConnection ( ) ;
597- const { bucketType, bucketName } =
612+ const { bucketType } =
598613 < SnowflakeDriverExportBucket > this . config . exportBucket ;
614+
615+ let bucketName : string ;
616+ let exportPrefix : string ;
617+ let path : string ;
618+
619+ if ( bucketType === 'azure' ) {
620+ ( { bucketName, path } = this . parseBucketUrl ( this . config . exportBucket ! . bucketName ) ) ;
621+ const pathArr = path . split ( '/' ) ;
622+ bucketName = `${ bucketName } /${ pathArr [ 0 ] } ` ;
623+ exportPrefix = pathArr . length > 1 ? `${ pathArr . slice ( 1 ) . join ( '/' ) } /${ tableName } ` : tableName ;
624+ } else {
625+ ( { bucketName, path } = this . parseBucketUrl ( this . config . exportBucket ! . bucketName ) ) ;
626+ exportPrefix = path ? `${ path } /${ tableName } ` : tableName ;
627+ }
628+
599629 const unloadSql = `
600- COPY INTO '${ bucketType } ://${ bucketName } /${ tableName } /'
630+ COPY INTO '${ bucketType } ://${ bucketName } /${ exportPrefix } /'
601631 FROM ${ tableName }
602632 ${ this . exportOptionsClause ( options ) } ` ;
603633 const result = await this . execute < UnloadResponse [ ] > (
@@ -695,36 +725,50 @@ export class SnowflakeDriver extends BaseDriver implements DriverInterface {
695725 * Returns an array of signed URLs of the unloaded csv files.
696726 */
697727 private async getCsvFiles ( tableName : string ) : Promise < string [ ] > {
698- const { bucketType, bucketName } =
728+ const { bucketType } =
699729 < SnowflakeDriverExportBucket > this . config . exportBucket ;
700730
701731 if ( bucketType === 's3' ) {
702- const cfg = < SnowflakeDriverExportAWS > this . config . exportBucket ;
732+ const { keyId, secretKey, region } = < SnowflakeDriverExportAWS > this . config . exportBucket ;
733+
734+ const { bucketName, path } = this . parseBucketUrl ( this . config . exportBucket ! . bucketName ) ;
735+ const exportPrefix = path ? `${ path } /${ tableName } ` : tableName ;
703736
704737 return this . extractUnloadedFilesFromS3 (
705738 {
706739 credentials : {
707- accessKeyId : cfg . keyId ,
708- secretAccessKey : cfg . secretKey ,
740+ accessKeyId : keyId ,
741+ secretAccessKey : secretKey ,
709742 } ,
710- region : cfg . region ,
743+ region,
711744 } ,
712745 bucketName ,
713- tableName ,
746+ exportPrefix ,
714747 ) ;
715748 } else if ( bucketType === 'gcs' ) {
716749 const { credentials } = (
717750 < SnowflakeDriverExportGCS > this . config . exportBucket
718751 ) ;
719- return this . extractFilesFromGCS ( { credentials } , bucketName , tableName ) ;
752+
753+ const { bucketName, path } = this . parseBucketUrl ( this . config . exportBucket ! . bucketName ) ;
754+ const exportPrefix = path ? `${ path } /${ tableName } ` : tableName ;
755+
756+ return this . extractFilesFromGCS ( { credentials } , bucketName , exportPrefix ) ;
720757 } else if ( bucketType === 'azure' ) {
721758 const { azureKey, sasToken, clientId, tenantId, tokenFilePath } = (
722759 < SnowflakeDriverExportAzure > this . config . exportBucket
723760 ) ;
761+
762+ const { bucketName, path } = this . parseBucketUrl ( this . config . exportBucket ! . bucketName ) ;
763+ const pathArr = path . split ( '/' ) ;
764+ const azureBucketPath = `${ bucketName } /${ pathArr [ 0 ] } ` ;
765+
766+ const exportPrefix = pathArr . length > 1 ? `${ pathArr . slice ( 1 ) . join ( '/' ) } /${ tableName } ` : tableName ;
767+
724768 return this . extractFilesFromAzure (
725769 { azureKey, sasToken, clientId, tenantId, tokenFilePath } ,
726- bucketName ,
727- tableName ,
770+ azureBucketPath ,
771+ exportPrefix ,
728772 ) ;
729773 } else {
730774 throw new Error ( `Unsupported export bucket type: ${ bucketType } ` ) ;
0 commit comments