How do I retreive a file from FILESTREAM? - sql-server

I have installed SQL Server Express on my machine and enabled and configured FILESTREAM to store and retrieve pdf's from SQL Server. I have successfully uploaded files to my table as BLOB's, but I am having difficulty retrieving them. My approach is to use a temporary directory to create the files in so I can open them. I have the following code:
I have configured ole automation
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
I have the following record in my table BusinessFiles:
FileID 415578C7-B058-49A5-A1E4-5A4E7218EA19
InvoiceID 99999
FileName file1
FileType pdf
ImageFile 0x255044462D312…
This next piece of code apparently will create the file from the filestream.
DECLARE
#FILE VARBINARY(MAX)
,#FILEPATH VARCHAR(MAX)
,#ObjectToken INT
,#FileType VARCHAR(MAX)
,#FILENAME VARCHAR(MAX)
Set #FILENAME = (SELECT [FileName] FROM BusinessFiles where FileID = '415578C7-B058-49A5-A1E4-5A4E7218EA19')
Set #FileType = (SELECT FileType FROM BusinessFiles where FileID = '415578C7-B058-49A5-A1E4-5A4E7218EA19')
SELECT #FILE = ImageFile FROM BusinessFiles where FileID = '415578C7-B058-49A5-A1E4-5A4E7218EA19'
Set #FILEPATH = CONCAT('C:\Users\user\Desktop\', #FILENAME,'.',#FileType)
EXEC sp_OACREATE 'ADODB.Stream', #ObjectToken OUTPUT
EXEC sp_OASetProperty #ObjectToken, 'Type', 1
EXEC sp_OAMethod #ObjectToken, 'Open'
EXEC sp_OAMethod #ObjectToken, 'Write', NULL, #FILE
EXEC sp_OAMethod #ObjectToken, 'SaveToFile', NULL, #FILEPATH, 2
EXEC sp_OAMethod #ObjectToken, 'Close'
EXEC sp_OADestroy #ObjectToken
Go
This code runs and I get the Commands Completed successfully message. However there is nothing in my folder C:\Users\user\Desktop. Is there something I'm missing? Thanks;
I've verified the file path is correct and all the set's are good.

Knowing the permissions of SQL Server will fix the SaveToFile Issue. In my case, I changed the SavetoFile Directory to C:\temp.

Related

How to read a XML file with SQL Server and AWS RSD?

I have a database on AWS RSD, I have a basic account of AWS. I found that I have to store the XML file on internet, then I have to read it from the URL.
So, I store the XML file into a Github repository and then I execute the query below. On my local server I have to execute a RECONFIG to activate some permissions.
It worked well on my local server, but in my database in AWS it didn't work. It prints a error of permissions of sp_OACreate, sp_OAMethod, sp_OAGetProperty, sp_OADestroy. Also I don't have permissions to execute sp_configure and RECONFIGURE.
I tried to change setting in the server page but I didn't found anything.
DECLARE
#url VARCHAR(1000),
#url2 VARCHAR(300),
#win INT,
#hr INT,
#Text VARCHAR(4000),
#xml XML
SET #url = 'https://raw.githubusercontent.com/KevinFallas03/FacturacionMunicipal_BD/master/Base%20de%20Datos/XML/Administradores.xml'--current file
SET #url2 = 'http://www.bnr.ro/nbrfxrates.xml'--test file
EXEC #hr = sp_OACreate 'WinHttp.WinHttpRequest.5.1', #win OUT
IF #hr <> 0 EXEC sp_OAGetErrorInfo #win
EXEC #hr = sp_OAMethod #win, 'Open', NULL, 'GET', #url, 'false'
IF #hr <> 0 EXEC sp_OAGetErrorInfo #win
EXEC #hr = sp_OAMethod #win, 'Send'
IF #hr <> 0 EXEC sp_OAGetErrorInfo #win
EXEC #hr = sp_OAGetProperty #win, 'ResponseText', #Text OUT
IF #hr <> 0 EXEC sp_OAGetErrorInfo #win
EXEC #hr = sp_OADestroy #win
IF #hr <> 0 EXEC sp_OAGetErrorInfo #win
SELECT #Text
SET #xml=CAST(#Text AS XML)
SELECT #xml
Query to activate permissions, it works on local database:
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
My intention is read a XML file from my database in AWS. So, if there is another way to do it, please tell me.
RDS offers many different types of database. It looks like you are using Microsoft SQL Server.
RDS is a managed database service. That means that you do not have full admin permissions on the database. You seem to be hoping to use OLE Automation, which requires the use of certain Stored Procedures like sp_OACreate.
OLE Automation is not supported in RDS Microsoft SQL Server. One alternative is to install and manage your own MS SQL Server running on EC2.
One option to solve the problem is to create a bucket on Amazon Storage S3, then upload the XML file on the bucket. Next you have to integrate AWS S3 buckets with AWS RDS SQL Server.
For complete guide to integrate AWS S3 buckets with AWS RDS SQL Server consulte here: https://www.sqlshack.com/integrating-aws-s3-buckets-with-aws-rds-sql-server/
With all set you can download the XML file with:
EXEC msdb.dbo.rds_download_from_s3
#s3_arn_of_file = 'arn:aws:s3:::<bucket-name>/<xml-name>.xml', --string with the ARN
#rds_file_path = 'D:\S3\<storage-name>\<xml-name>.xml', --where xml is saved
#overwrite_file = 1;
To check the status of the previuos task:
EXEC msdb..rds_task_status #task_id = <task_number>;
To parse the XML file:
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE()
FROM OPENROWSET(BULK 'D:\S3\<storage-name>\<xml-name>.xml', SINGLE_BLOB) AS x;

How Do I Find SSL Certificate Full Path And Use in a StoredProcedure to call an API

How do I find the ssl certificate path? That one installed by VisualStudio, to be more expecific when we use .NET Core and run "dotnet dev-certs https --trust" at the console...
Could you guys help me pls?
Everything I've tried drop me in the certificate name only. I could not see the path to set on that pattern 'LOCAL_MACHINE\My\'Certificate Name'.
I want to pass it ike a parameter on the SQL code below:
EXEC #hResult = sp_OAMethod
#Object,
'setOption',
NULL,
3,
'LOCAL_MACHINE\My\'Certificate Name';
Just because I'm trying to call a API using SQL Server and I need to use that SSL certificate to run my tests.
Could you guys help me pls?
The return message I've got untill now says that it's not a valid certificate but I think it's bacause I'm not finding the full path.
I don't know if I made this clear... Just summarazing... I need to do this by sql and I need to know how to use SSL certificate path as a parameter.
There follows the code I'm using:
begin
declare #test as int;
declare #Object as int;
declare #hResult as nvarchar(256)
declare #Response as nvarchar(256)
exec #test = sp_OACreate 'MSXML2.XMLHTTP', #Object out;
select #test, #Object;
EXEC #hResult = sp_OAMethod
#Object,
'setOption',
NULL,
3,
'LOCAL_MACHINE\My\'Certificate Name';
exec #test = sp_OAMethod #Object, 'open', null, 'get',
'https://localhost:44351/api/v1/account/3215', 'false'
select #test
exec #test = sp_OAMethod #Object, 'send'
IF #test <> 0
BEGIN
EXEC sp_OAGetErrorInfo #object
RETURN
END
exec sp_OAMethod #Object, 'responseText', #Response OUTPUT;
end
print #Response
I had the same problem using the same VS-generated cert. I could find the cert called "localhost" in the certmgr ("Personal"->"Certificates"->localhost), installed when I began the project. I added this line:
EXEC #hr = sp_OAMethod #Object,'setOption',NULL,3,'LOCAL_MACHINE\My\localhost';
and it worked.
However, i found I could change the line to anything and it still worked, ie:
EXEC #hr = sp_OAMethod #Object,'setOption',NULL,3,'LOCAL_MACHINE\My\bogusfake';
So I cannot promise that will work for you, nor explain how/why, but I am able to proceed with the project of posting from a stored procedure to a local api. The only difference I see between my code and yours is I didn't surround the cert name in quotes.

Microsoft SQL Server and varbinary data type conversion

I'm not familiar with Microsoft SQL Server and the varbinary(max) data type but the database which I need to use stores images as that.
My question is what do I need to do to read that data back and convert it back to readable image type?
So far I have done was
SELECT CONVERT(VARCHAR(max), [IMAGE], 2)
FROM [demo].[dbo].[DOCIMAGES]
WHERE [TITLE] = 'test.jpg'
but what I have got it was almost the same just removed 0x from the beginning of that data when
SELECT CONVERT(VARCHAR(max), [IMAGE], 0)
FROM [demo].[dbo].[DOCIMAGES]
WHERE [TITLE] = 'test.jpg'
returns value as Lead.
Then I have tried to do stored procedure which will save it to a file like:
DECLARE #ImageData VARBINARY (max);
DECLARE #Path2OutFile NVARCHAR (2000);
DECLARE #Obj INT
SET NOCOUNT ON
SELECT #ImageData = (
SELECT CONVERT(VARBINARY(max), [IMAGE], 1)
FROM [demo].[dbo].[DOCIMAGES]
WHERE [TITLE] = 'test.jpg'
);
SET #Path2OutFile = CONCAT (
'C:\Users\MyPC\Downloads'
,'\'
,'test.jpg'
);
BEGIN TRY
EXEC sp_OACreate 'ADODB.Stream' ,#Obj OUTPUT;
EXEC sp_OASetProperty #Obj ,'Type',1;
EXEC sp_OAMethod #Obj,'Open';
EXEC sp_OAMethod #Obj,'Write', NULL, #ImageData;
EXEC sp_OAMethod #Obj,'SaveToFile', NULL, #Path2OutFile, 2;
EXEC sp_OAMethod #Obj,'Close';
EXEC sp_OADestroy #Obj;
END TRY
BEGIN CATCH
EXEC sp_OADestroy #Obj;
END CATCH
SET NOCOUNT OFF
but that fails with an error:
SQL Server blocked access to procedure 'sys.sp_OADestroy' of component
'Ole Automation Procedures' because this component is turned off as
part of the security configuration for this server. A system
administrator can enable the use of 'Ole Automation Procedures' by
using sp_configure.
Problem is that I'm not the admin of that server.
In that case can I dump content of the [IMAGE] to a file and then use Java or PHP to stream that content and save it as an image file?

SQL Server - Ole Automation Procedures Inside Queue Activation Procedure

I want to call a Webservice when a row inserted into a table.
So I enable Ole Automation Procedures, call Webservice like below and everything goes fine.
--enabling 'Ole Automation Procedures'
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
--calling Webservice
create PROCEDURE callwebservice
#url varchar(128)=null
AS
Declare #Object as Int;
Declare #ResponseText as varchar(8000);
Exec sp_OACreate 'MSXML2.ServerXMLHttp.3.0', #Object OUT;
Exec sp_OAMethod #Object, 'open', NULL, 'get', #url ,'false'
Exec sp_OAMethod #Object, 'send'
Exec sp_OAGetProperty #Object, 'ResponseText', #ResponseText OUTPUT
Exec sp_OADestroy #Object
return
But, Since Webservice call is extremely lengthy, I need to do it with Service Broker. so I create a new stored-procedure and pass it as an activation parameter of my Queue like this:
--declare procedure
create procedure handleNewMessageInQueue
as
begin
DECLARE #TargetDlgHandle UNIQUEIDENTIFIER
DECLARE #ReplyMessage xml
DECLARE #ReplyMessageName
BEGIN TRAN;
receive top(1)
#TargetDlgHandle =Conversation_handle
,#ReplyMessage = Message_Body
,#ReplyMessageName = Message_Type_Name
from newUserQueue;
if #ReplyMessageName = '//goodType'
begin
exec callwebservice 'http://path/to/my/webservice'
end
commit tran;
end
--declare queue
create queue myQueue
with ACTIVATION (
PROCEDURE_NAME = handleNewMessageInQueue,
MAX_QUEUE_READERS =100,
EXECUTE AS owner
);
But this time, no request sent. I checked and I realize that the sp_OACreate method does not create the Object since the value of #Object is null after execution of it. Also when I call sp_OAGetErrorInfo to get error info, everything is null
I'm using SQL Server 2016
I appreciate if you can find the problem here.
EDIT
I found that if I enable TRUSTWORTHY on that database everything goes fine. but enabling it can open security hole.
is there any better way to do that?

Save SQL Server Filestream data to file

I use SQL Server and enable FileStream on my database. In order to save my file to filestream table I use below code from SSMS :
INSERT INTO dbo.mytable( Data )
SELECT *
FROM OPENROWSET(BULK 'C:\Temp\image.png', SINGLE_BLOB) AS z
I look for a SQL query to save FileStream data from table to special path.
How can do this.
I look for SQL Server query and I don't want use tools application or programming in .Net, Delphi and etc.
thanks in advance
How about this? It's a pure SQL approach to writing a file to disk, but it does you the command shell, which opens up a security risk on the server.
declare #writetofile varchar(max)
select #writetofile = 'osql -U -P -S -Q"select ColumnName from yourtable" -o"c:\SomeFolder\MyFile.bin"
exec master..xp_cmdshell #writetofile
Update:
This is not the easiest thing to get running, but try running this in a powerscript or .bat file:
Rem CD command sets the execution folder
cd C:\Program Files\Microsoft SQL Server\90\Tools\binn
rem SQLCmd sets the command using SQLCmd.exe program
rem -S = server name, -d database name, -U is username, -P is password, -Q is query to run, -o is output to
SQLCMD -S ServerName\InstanceName -d DBName -U sa -P MyPassword -Q"select top 1 FieldName from TableName" -o"c:\temp\MyFile.txt"
This post may help you:
Script to save varbinary data to disk
DECLARE #SQLIMG VARCHAR(MAX),
#IMG_PATH VARBINARY(MAX),
#TIMESTAMP VARCHAR(MAX),
#ObjectToken INT
DECLARE IMGPATH CURSOR FAST_FORWARD FOR
SELECT csl_CompanyLogo from mlm_CSCompanySettingsLocalizations
OPEN IMGPATH
FETCH NEXT FROM IMGPATH INTO #IMG_PATH
WHILE ##FETCH_STATUS = 0
BEGIN
SET #TIMESTAMP = 'd:\' + replace(replace(replace(replace(convert(varchar,getdate(),121),'-',''),':',''),'.',''),' ','') + '.bmp'
PRINT #TIMESTAMP
PRINT #SQLIMG
EXEC sp_OACreate 'ADODB.Stream', #ObjectToken OUTPUT
EXEC sp_OASetProperty #ObjectToken, 'Type', 1
EXEC sp_OAMethod #ObjectToken, 'Open'
EXEC sp_OAMethod #ObjectToken, 'Write', NULL, #IMG_PATH
EXEC sp_OAMethod #ObjectToken, 'SaveToFile', NULL, #TIMESTAMP, 2
EXEC sp_OAMethod #ObjectToken, 'Close'
EXEC sp_OADestroy #ObjectToken
FETCH NEXT FROM IMGPATH INTO #IMG_PATH
END
CLOSE IMGPATH
DEALLOCATE IMGPATH

Resources