Azure blob 到 Azure SQL 数据库:无法批量加载,因为无法打开文件 "xxxx.csv"。操作系统错误代码 5(访问被拒绝。)

Azure blob to Azure SQL Database: Cannot bulk load because the file "xxxx.csv" could not be opened. Operating system error code 5(Access is denied.)

我正在尝试在 azure sql 数据库中批量加载 azure blob 存储中的一些数据。 文件内容为:

 customer,age,gender
'C1093826151','4','M'
'C352968107','2','M'
'C2054744914','4','F'

文件位于名为 silver 的容器中。 在银容器中我有 File1.fmt 内容是:

14.0  
3
1       SQLCHAR       0       7       ","      1     customer       ""  
2       SQLCHAR       0       100     ","      2     age            SQL_Latin1_General_CP1_CI_AS 
3       SQLCHAR       0       100     "\r\n"   3     gender         SQL_Latin1_General_CP1_CI_AS

我有额外的行添加到 fmt 文件的末尾。

我创建了一个 SAS 令牌,将全部启用并允许,如下面的屏幕截图所示:

datalake的防火墙规则如下图:

下面是我的 sql 脚本(我删除了 SAS 令牌开头的 ?,因为我的银容器是 public,我知道我应该需要 SAS 令牌):

CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'safepassword';
go
DROP EXTERNAL DATA SOURCE MyAzureInvoices

DROP DATABASE SCOPED CREDENTIAL UploadInvoices

CREATE DATABASE SCOPED CREDENTIAL UploadInvoices
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = 'sv=2019-12-12**********************************88%3D'; -- dl

--DROP EXTERNAL DATA SOURCE MyAzureInvoices

CREATE EXTERNAL DATA SOURCE MyAzureInvoices
    WITH (
        TYPE = BLOB_STORAGE,
        LOCATION = 'https://mydatalake.blob.core.windows.net/silver',
        CREDENTIAL = UploadInvoices
    );

登陆table:

CREATE TABLE [ext].[customer](
    [customer_id] [int] IDENTITY(1,1) NOT NULL,
    [customer] [varchar](100) NOT NULL,
    [age] [int] NOT NULL,
    [gender] [varchar](50) NOT NULL
) ON [PRIMARY]
GO

这些是我尝试将文件加载到 sql 数据库的方法:

-- 1
    SELECT * FROM OPENROWSET(
   BULK 'bs140513_032310-demo.csv',
   DATA_SOURCE = 'MyAzureInvoices',
   FORMAT = 'CSV',
   FORMATFILE='File1.fmt',
   FORMATFILE_DATA_SOURCE = 'MyAzureInvoices'
   ) AS DataFile;   
-- 2    
    go
    SELECT * FROM OPENROWSET(
   BULK 'bs140513_032310-demo.csv',
   DATA_SOURCE = 'MyAzureInvoices',
   SINGLE_CLOB) AS DataFile;
   go
-- 3
BULK INSERT ext.customer
FROM 'bs140513_032310-demo.csv'
WITH (
DATA_SOURCE = 'MyAzureInvoices', FORMAT = 'CSV' );

他们都报同样的错误:

Msg 4861, Level 16, State 1, Line 2
Cannot bulk load because the file "bs140513_032310-demo.csv" could not be opened. Operating system error code 5(Access is denied.).

试了3天,我迷路了。谢谢你的帮助 注意:

断开连接时,它可以访问文件:

*

mydatalake是假的,但我可以实名访问

我认为此错误消息具有误导性。
我创建了与您相同的测试,但遇到了相同的错误。
但是在我编辑了 bs140513_032310-demo.csvFile1.fmt 之后,效果很好。

  1. 我把 bs140513_032310-demo.csv 改成这样:

  2. 我这样更改了 File1.fmt,我将 cutomer 列 的长度从 7 更改为 100,age 列 长度从 100 到 7 :

14.0  
3
1       SQLCHAR       0       100       ","      1     customer       ""
2       SQLCHAR       0       7         ","      2     age            SQL_Latin1_General_CP1_CI_AS
3       SQLCHAR       0       100       "\r\n"   3     gender         ""
  1. 我使用如下语句查询:
   SELECT * FROM OPENROWSET(
   BULK 'bs140513_032310-demo.csv',
   DATA_SOURCE = 'MyAzureInvoices',
   FORMAT = 'CSV',
   FORMATFILE='File1.fmt',
   FORMATFILE_DATA_SOURCE = 'MyAzureInvoices'
   ) AS DataFile; 

结果显示:

  1. 不要 BULK INSERT 直接插入到您真正的 table 中。
  • 我总是从 CSV 文件 table ext.customer_Staging(没有 IDENTITY 列)中插入
  • 可能编辑/清理/操作您导入的数据
  • 然后使用 T-SQL 语句将数据复制到真正的 table,例如:
INSERT into  ext.customer_Staging with (TABLOCK) (customer, age, gender)
   SELECT * FROM OPENROWSET(
   BULK 'bs140513_032310-demo.csv',
   DATA_SOURCE = 'MyAzureInvoices',
   FORMAT = 'CSV',
   FORMATFILE='File1.fmt',
   FORMATFILE_DATA_SOURCE = 'MyAzureInvoices'
   ) AS DataFile;
   go

INSERT INTO ext.customer(Name, Address) 
   SELECT customer, age, gender
   FROM ext.customer_Staging