Quick question about downloading Blobs from azure storage. I'm a little lost on how this works exactly, here's my code:
Try
Dim accountname As String = "macroqc"
Dim accountkey As String = My.Settings.Storagekey1
Dim creds As StorageCredentials = New StorageCredentials(accountname, accountkey)
Dim account As CloudStorageAccount = New CloudStorageAccount(creds, useHttps:=True)
Dim client = account.CreateCloudBlobClient()
Dim container As CloudBlobContainer = client.GetContainerReference("smallequipment1certifications")
Dim blob As CloudBlockBlob = container.GetBlockBlobReference(filenamelbl.Text)
Using MemoryStream = New MemoryStream()
blob.DownloadToStream(MemoryStream)
My.Settings.downloadsource = System.Text.Encoding.UTF8.GetString(MemoryStream.ToArray())
equipmentpdf.src = filenamelbl.Text
End Using
Catch ex As Exception
MessageBox.Show("Sorry an error has occured while opening your file: " & Environment.NewLine & ex.ToString, "Download Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
So I am getting an error when I run this which looks like:
So when it uploads the file to Azure storage it saves blob.Uri.AbsoluteUri & blob.Uri.AbsolutePath to the database. Am I going down the right path using this method to download the file? The Blobs are private do I need an SAS to download? I am confused and am having a really hard time finding useful documenation on this! Can someone help point me in the right direction please!!
Thanks everyone!
The reason you're getting this error is because GetBlockBlobReference expects the name of the blob (and not the full blob URL) and it creates a URL based on that. Since you're passing the complete URL, it still creates a URL but includes the blob URL. If you put a breakpoint and check the blob object's URL property, you will see something like:
https://accountname.blob.core.windows.net/containername/https://accountname.blob.core.windows.net/containername/filename
Since the blob by the name https://accountname.blob.core.windows.net/containername/filename does not exist in the container, you're getting a 404 (Not Found) error. Please provide just the file name in GetBlockBlobReference and things should work just fine.
Related
i'm working in a console app that is going to be executed in a server every week, basically it generates a report in excel and then it has to upload it to a folder in dropbox, i've trying a lot of stuff for that last part i finally got this code that does not work but doesn't throw any exception (before i had one that throw and invalid folder format)
Dim _path As String
_path = "/Pruebas/" & Path.GetFileName(FilePath)
Try
Dim rawData = File.ReadAllBytes(FilePath)
Dim str = System.Text.Encoding.Default.GetString(rawData)
Using mem = New MemoryStream(Encoding.UTF8.GetBytes(str))
Dim Up = I.Files.UploadAsync(_path, body:=mem)
MsgBox("Successfully Uploaded")
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
it doesn't throw exception but also doens't work, any help i'll be thankfull.
try this.
Dim Up = I.Files.UploadAsync(_path, WriteMode.Overwrite.Instance, body:=mem)
Link to documentation: http://dropbox.github.io/dropbox-sdk-dotnet/html/M_Dropbox_Api_Files_Routes_FilesUserRoutes_UploadAsync_1.htm
I am trying to copy the contents of an embedded file to a string in Visual Basic using Visual Studio 2013. I already have the resource (Settings.xml) imported and set as an embedded resource. Here is what I have:
Function GetFileContents(ByVal FileName As String) As String
Dim this As [Assembly]
Dim fileStream As IO.Stream
Dim streamReader As IO.StreamReader
Dim strContents As String
this = System.Reflection.Assembly.GetExecutingAssembly
fileStream = this.GetManifestResourceStream(FileName)
streamReader = New IO.StreamReader(fileStream)
strContents = streamReader.ReadToEnd
streamReader.Close()
Return strContents
End Function
When I try to save the contents to a string by using:
Dim contents As String = GetFileContents("Settings.xml")
I get the following error:
An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll
Additional information: Value cannot be null.
Which occurs at line:
streamReader = New IO.StreamReader(fileStream)
Nothing else I've read has been very helpful, hoping someone here can tell me why I'm getting this. I'm not very good with embedded resources in vb.net.
First check fileStream that its not empty as it seems its contains nothing that's why you are getting a Null exception.
Instead of writing to file test it by using a msgBox to see it its not null.
fileStream is Nothing because no resources were specified during compilation, or because the resource is not visible to GetFileContents.
After fighting the thing for hours, I discovered I wasn't importing the resource correctly. I had to go to Project -> Properties -> Resources and add the resource from existing file there, rather than importing the file from the Solution Explorer. After adding the file correctly, I was able to write the contents to a string by simply using:
Dim myString As String = (My.Resources.Settings)
Ugh, it's always such a simple solution, not sure why I didn't try that first. Hopefully this helps someone else because I saw nothing about this anywhere else I looked.
Trying to download a website to a file using WebClient DownloadFile works fine as long as I have a preset filename for it. If the filename is variable it throws an exception and I can't work out why.
dim link as string = "http://google.com"
Dim myWebClient As New System.Net.WebClient
myWebClient.DownloadFile(link, link & ".html")
If the output is set to "site.html" it'll work just fine but I can't get it to accept a variable.
I'm passing the variable link to the sub and I want it to save that site to The-Sitename.html. I'm guessing there is something really simple I'm missing here?
You cant save a file with the website extension included in the URL
Try using the following code to exclude common extensions to hopefully fix the issue
link = link.Replace("http://", "")
link = link.Replace("https://", "")
myWebClient.DownloadFile(link, link & ".html")
I need download a CSV file and then read it. Here is my code:
tickerValue = "goog"
Dim strURL As String = "http://ichart.yahoo.com/table.csv?s=" & tickerValue
Dim strBuffer As String = RequestWebData(strURL)
Using streamReader = New StreamReader(strBuffer)
Using reader = New CsvReader(streamReader)
I keep getting this error: An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: Illegal characters in path.
What am I doing wrong?
Additional Info
In another part of my program I use this code and it works fine.
Address = http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=AMEX&render=download
Dim strBuffer As String = Historical_Stock_Prices.RequestWebData(Address)
Using streamReader = New StringReader(strBuffer)
Using reader = New CsvReader(streamReader)
Isn't my second code the same concept as my problem code?
you are giving it, essentially, a web url. somewhere in your code, it does not support the web url. it could be the streamreader. it could be the CsvReader.
what line of code does this point to?
the best bet is to save the file TO DISK, then read from disk.
UPDATE
here is an example to SAVE to disk:
using writer as new StreamWriter("C:\Test.csv")
writer.Write(strBuffer)
writer.Close()
end using
here is an example to READ from disk:
using strReader as new StreamReader("C:\Test.csv")
' this code is presumably how it works for reading into the CsvReader:
using reader as new CsvReader(strReader)
' now do your thing
end using
strReader.Close()
end using
I am new to Amazon S3 and after searching through Amazon & many sites I have made some progress.
I am creating a windows From application to upload the files. I tried with following code and it does not upload the file and I don't get any errors.
If I comment the .withKey property than file is uploaded the root of the bucket, but instead I would like to upload to a specific folder within a folder.
Try
Dim fileTransferUtility As New TransferUtility(client)
Dim request As TransferUtilityUploadRequest = New TransferUtilityUploadRequest
Dim S3_KEY As String = "MitsTest/CVs" 'I want to upload file to CVs folder
With request
.BucketName = "mybucketname"
.WithKey(S3_KEY)
.WithFilePath("C:\Users\mitesh\Desktop\s3.txt")
.WithTimeout(FIVE_MINUTES)
End With
fileTransferUtility.Upload(request)
Catch ex As Exception
Finally
End Try
I have also tried using following and same result, I dont get any error but file does not get uploaded.
Dim Filepath = "C:\Users\mitesh\Desktop\s3.txt"
Dim FolderName = "MitsTest/CVs/"
Dim PutRequest As New PutObjectRequest()
Try
With PutRequest
.WithBucketName("mybucketname")
'.WithKey(FolderName + "/" + FilePath)
.WithKey(FolderName)
.WithFilePath(FilePath)
.StorageClass = S3StorageClass.Standard
.ServerSideEncryptionMethod = ServerSideEncryptionMethod.None
'.WithContentBody(String.Empty)
Dim response As S3Response = client.PutObject(PutRequest)
response.Dispose()
End With
Catch ex As Exception
Stop
End Try
if I uncomment this line '.WithKey(FolderName + "/" + FilePath) and remove the forward slash(/) from folder name, then I get "The request signature we calculated does not match the signature you provided. Check your key and signing method." error.
What am i missing here or doing wrong? Any help would be really appreciated.
regards
Mitesh
The reason you get the signing error is that the back slash characters in "C:\users.." are invalid for key names. You should either replace them with forward slash or use a different value for the key name.
In your first example, you are actually trying to upload to the "MitsTest/CVs" key. You would need to append the name of the file like this:
.WithKey(S3_KEY+ "/" + "s3.txt")