简体   繁体   English

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.)

I am trying to bulk load in azure sql database some data in an azure blob storage.我正在尝试在 azure sql 数据库中批量加载 azure blob 存储中的一些数据。 The file content is:文件内容为:

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

the file is in a container called silver .该文件位于名为silver的容器中。 in the silver container I have the File1.fmt which content is:在银色容器中,我有 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

I have the extra line add the end of the fmt file.我有额外的行添加 fmt 文件的末尾。

I have created a SAS token will all enabled and allowed like the screenshot below:我创建了一个 SAS 令牌,将全部启用并允许,如下面的屏幕截图所示: 在此处输入图像描述

The firewall rules on datalake are as the picture below: datalake上的防火墙规则如下图:

在此处输入图像描述

Below are my sql scripts (I removed the? at the beginning of the SAS token, as my silver container is public, I know I should need the SAS token):下面是我的 sql 脚本(我删除了 SAS 令牌开头的?,因为我的银容器是公开的,我知道我应该需要 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
    );

Landing 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

and these are the ways I tried to load the file into the sql database:这些是我尝试将文件加载到 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' );

They all give the same error:他们都给出了同样的错误:

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.).

I have tried for 3 days and I am lost.我已经尝试了 3 天,但我迷路了。 Thanks for your help NB:感谢您的帮助注意:

While being disconnected, it can access the files:断开连接时,它可以访问文件:

* *

mydatalake is fake, but I can access with the real name mydatalake是假的,但我可以实名访问

I think this error message is misleading.我认为此错误消息具有误导性。
I've created a same test as you, and encountered the same error.我创建了与您相同的测试,并遇到了相同的错误。
But after I edited the bs140513_032310-demo.csv and File1.fmt , it works well.但是在我编辑bs140513_032310-demo.csvFile1.fmt ,它运行良好。

  1. I changed the bs140513_032310-demo.csv like this:我像这样更改了bs140513_032310-demo.csv 在此处输入图像描述

  2. I changed the File1.fmt like this, I changed the cutomer column length from 7 to 100 and age column length from 100 to 7:我像这样更改了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. I use the following statement to query:我使用如下语句查询:
   SELECT * FROM OPENROWSET(
   BULK 'bs140513_032310-demo.csv',
   DATA_SOURCE = 'MyAzureInvoices',
   FORMAT = 'CSV',
   FORMATFILE='File1.fmt',
   FORMATFILE_DATA_SOURCE = 'MyAzureInvoices'
   ) AS DataFile; 

The result shows:结果显示:
在此处输入图像描述

  1. Don't BULK INSERT into your real tables directly.不要直接将BULK INSERT 插入到您的真实表中。
  • I would always insert into a staging table ext.customer_Staging (without the IDENTITY column) from the CSV file我总是会从 CSV 文件插入临时表 ext.customer_Staging(没有 IDENTITY 列)
  • possibly edit / clean up / manipulate your imported data可能编辑/清理/操作您导入的数据
  • and then copy the data across to the real table with a T-SQL statement like:然后使用如下 T-SQL 语句将数据复制到实际表中:
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法批量加载,因为文件“ \\\\” <UC Path> \\\\ Test.csv”无法打开。 操作系统错误代码1240 - Cannot bulk load because the file “\\<UC Path>\\Test.csv” could not be opened. Operating system error code 1240 由于无法打开文件,因此无法批量加载。 操作系统错误代码5 - Cannot bulk load because the file could not be opened. Operating System Error Code 5 SQL备份错误(操作系统错误5(拒绝访问。)) - SQL backup error (Operating system error 5(Access is denied.)) SQL 服务器操作系统错误 5:“5(拒绝访问。)” - SQL Server Operating system error 5: "5(Access is denied.)" Azure Blob 无法批量加载 - Azure Blob Cannot Bulk Load SQL Server 2008 - 附加没有日志的 mdf - 操作系统错误 5:5(访问被拒绝。) - SQL Server 2008 - Attach mdf without log - Operating system error 5: 5(Access is denied.) 无法打开物理文件。 操作系统错误 5:“5(拒绝访问。)” - Unable to open the physical file. Operating system error 5: "5(Access is denied.)" sp_attach_single_file_db 错误:因操作系统错误 5 失败(访问被拒绝。) - sp_attach_single_file_db Error: failed with the operating system error 5(Access is denied.) 在SQL存储过程中访问Azure Blob存储时如何更正“操作系统错误代码12007” - How to correct 'Operating system error code 12007' when accessing Azure blob storage in a SQL stored procedure SQL批量插入-访问被拒绝(错误代码5) - SQL Bulk Insert - Access is denied (Error Code 5)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM