简体   繁体   English

从 Azure Blob 存储读取文件到 Azure SQL 数据库

[英]read file from Azure Blob Storage into Azure SQL Database

I have already tested this design using a local SQL Server Express set-up.我已经使用本地 SQL Server Express 设置测试了此设计。

I uploaded several .json files to Azure Storage In SQL Database, I created an External Data source:我上传了几个 .json 文件到 Azure Storage 在 SQL 数据库中,我创建了一个外部数据源:

CREATE EXTERNAL DATA SOURCE MyAzureStorage WITH (TYPE = BLOB_STORAGE, LOCATION = 'https://mydatafilestest.blob.core.windows.net/my_dir );

Then I tried to query the file using my External Data Source:然后我尝试使用我的外部数据源查询文件:

select *
from OPENROWSET
 (BULK 'my_test_doc.json', DATA_SOURCE = 'MyAzureStorage', SINGLE_CLOB) as data

However, this failed with the error message "Cannot bulk load. The file "prod_EnvBlow.json" does not exist or you don't have file access rights."但是,此操作失败并显示错误消息“无法批量加载。文件“prod_EnvBlow.json”不存在或您没有文件访问权限。

Do I need to configure a DATABASE SCOPED CREDENTIAL to access the file storage, as described here?如此处所述,我是否需要配置 DATABASE SCOPED CREDENTIAL 才能访问文件存储? https://docs.microsoft.com/en-us/sql/t-sql/statements/create-database-scoped-credential-transact-sql https://docs.microsoft.com/en-us/sql/t-sql/statements/create-database-scoped-credential-transact-sql

What else can anyone see that has gone wrong and I need to correct?还有什么人可以看到出错了,我需要纠正吗?

OPENROWSET is currently not supported on Azure SQL Database as explained in this documentation page .如本文档页面中所述,Azure SQL 数据库当前不支持OPENROWSET You may use BULK INSERT to insert data into a temporary table and then query this table.您可以使用BULK INSERT将数据插入到临时表中,然后查询该表。 See this page for documentation on BULK INSERT .有关BULK INSERT文档,请参阅此页面

Now that OPENROWSET is in public preview, the following works.现在OPENROWSET处于公共预览阶段,以下工作正常。 Nb the key option is in case your blob is not public.注意,关键选项是在您的 blob 不公开的情况下。 I tried it on a private blob with the scoped credential option and it worked.我在具有范围凭据选项的私有 blob 上进行了尝试,并且成功了。 nnb if you are using a SAS key make sure you delete the leading ? nnb 如果您使用的是 SAS 密钥,请确保删除前导? so the string should start with sv as shown below.所以字符串应该以sv开头,如下所示。

Make sure the blobcontainer/my_test_doc.json section specifies the correct path eg container/file .确保blobcontainer/my_test_doc.json部分指定了正确的路径,例如container/file

CREATE DATABASE SCOPED CREDENTIAL MyAzureBlobStorageCredential
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = 'sv=2017****************';

CREATE EXTERNAL DATA SOURCE MyAzureBlobStorage
WITH ( TYPE = BLOB_STORAGE,
   LOCATION = 'https://yourstorage.blob.core.windows.net',
   CREDENTIAL= MyAzureBlobStorageCredential);

DECLARE @json varchar(max)
SELECT @json = BulkColumn FROM OPENROWSET(BULK 'blobcontainer/my_test_doc.json', 
SINGLE_BLOB, DATA_SOURCE = 'MyAzureBlobStorage',
            FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage') as j;

select @json;

More detail provided in these docs这些文档中提供了更多详细信息

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM