使用 BULK INSERT 将文件从 Azure Blob 存储加载到 Azure SQL 数据库

Loading file from Azure Blob Storage into Azure SQL Database using BULK INSERT

在此公告之后https://azure.microsoft.com/en-gb/updates/preview-loading-files-from-azure-blob-storage-into-sql-database/

我尝试了提供的示例 in this GitHub sample 并收到以下错误,

-- Create credential with Azure Blob SAS
CREATE DATABASE SCOPED CREDENTIAL xxxstorcred
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = '?sv=2015-12-11&ss=bfqt&srt=sco&sp=rwdl&st=2017-03-14T17%3A52%3A00Z&se=2017-05-31T16%3A52%3A00Z&sig=f45435435TzrsdsdsdC8wonjDMBG0T0GJj717XetLEWReZ64eOrQE%3D';

-- Create external data source with with the roow URL of the Blob storage Account and associated credential.
CREATE EXTERNAL DATA SOURCE xxxstor
WITH (  TYPE = BLOB_STORAGE, 
        LOCATION = 'https://xxxstor.blob.core.windows.net', 
        CREDENTIAL= xxxstorcred);
--CREATE DESTINATION TABLE (if not exists)
DROP TABLE IF EXISTS Product;
GO

CREATE TABLE dbo.Product(
    Name nvarchar(50) NOT NULL,
    Color nvarchar(15) NULL,
    Price money NOT NULL,
    Size nvarchar(5) NULL,
    Quantity int NULL,
    Data nvarchar(4000) NULL,
    Tags nvarchar(4000) NULL
)
GO

--LOAD

-- INSERT CSV file into Product table
BULK INSERT Product
FROM 'random/product.csv' --random is the container name
WITH (  DATA_SOURCE = 'xxxstor',
        FORMAT='CSV', CODEPAGE = 65001, --UTF-8 encoding
        FIRSTROW=2,
        TABLOCK); 

Cannot bulk load because the file "random/product.csv" could not be opened. Operating system error code 1117(The request could not be performed because of an I/O device error.).

我错过了什么?

我已经尝试了您提供的 github 示例中的 t-sql。它工作正常。从我的测试来看,有两种可能会导致这个错误:

1) 容器名称不正确

2) SAS SECRET 不正确

根据您的描述,我认为您的 SAS 机密不正确。这是您使用的秘密:

SECRET = '?sv=2015-12-11&ss=bfqt&srt=sco&sp=rwdl&st=2017-03-14T17%3A52%3A00Z&se=2017-05-31T16%3A52%3A00Z&sig=f45435435TzrsdsdsdC8wonjDMBG0T0GJj717XetLEWReZ64eOrQE%3D';

作为我的测试,我们需要删除“?”。请尝试以下秘密:

SECRET = 'sv=2015-12-11&ss=bfqt&srt=sco&sp=rwdl&st=2017-03-14T17%3A52%3A00Z&se=2017-05-31T16%3A52%3A00Z&sig=f45435435TzrsdsdsdC8wonjDMBG0T0GJj717XetLEWReZ64eOrQE%3D';

如何生成SAS,请参考this article

在我的例子中,这是 SAS 令牌的另一个问题:它无效

我创建了一个带有默认开始日期(即我当前时区的当前时间)的令牌。但后来它正在根据 GMT 进行评估,这落后于我的时间。

您可以在 Microsoft Azure Storege Explorer 中轻松测试 SAS 令牌。在这种情况下,它将显示如下消息:

解决方法是修改开始时间,例如至 00:00:00.