How to create an Azure AppendBlob from node.js - javascript

I have installed the npm azure-storage package.
On Azure I have created a Storage Account and a container.
I then try to create an Append Blob:
const azure = require('azure-storage');
const service = azure.createBlobService("[ACCOUNT]", "[KEY]");
service.createAppendBlobFromText("[CONTAINER]",
"some-blob-name",
"some-text",
{},
(err, result) => {
console.log('err ->',err);
console.log('result ->',result);
});
The result of calling this is:
err -> { Error
at Function.StorageServiceClient._normalizeError (/[REMOVED]/node_modules/azure-storage/lib/common/services/storageserviceclient.js:1191:23)
at BlobService.StorageServiceClient._processResponse (/[REMOVED]/node_modules/azure-storage/lib/common/services/storageserviceclient.js:738:50)
at Request.processResponseCallback [as _callback] (/[REMOVED]/node_modules/azure-storage/lib/common/services/storageserviceclient.js:311:37)
at Request.self.callback (/[REMOVED]/node_modules/request/request.js:186:22)
at emitTwo (events.js:125:13)
at Request.emit (events.js:213:7)
at Request.<anonymous> (/[REMOVED]/node_modules/request/request.js:1163:10)
at emitOne (events.js:115:13)
at Request.emit (events.js:210:7)
at IncomingMessage.<anonymous> (/[REMOVED]/node_modules/request/request.js:1085:12)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'StorageError',
message: 'Append blobs are not supported.\nRequestId:ed1777f4-601c-00cf-19a0-bb77ba000000\nTime:2018-03-14T14:25:50.8138962Z',
code: 'BlobTypeNotSupported',
statusCode: 400,
requestId: 'ed1777f4-601c-00cf-19a0-bb77ba000000' }
result -> null
I have not been able to find anything, when searching for the error.
Am I missing something here?

Please check the redundancy kind of the storage account in which you're trying to create this blob.
Blob type support varies by the storage account redundancy kind.
For example, ZRS Classic redundancy kind of storage account only supports Block Blob while Premium LRS redundancy kind of storage account only supports Page Blob.

Related

Firebase Functions and Autodesk Forge integration

I have a problem with a fetch request inside Firebase Functions for the Autodesk Forge Token.
Here is the error that is showing on functions registrations:
FetchError: request to https://developer.api.autodesk.com/authentication/v1/authenticate failed,
reason: getaddrinfo EAI_AGAIN developer.api.autodesk.com:443
at ClientRequest.<anonymous> (/srv/node_modules/node-fetch/lib/index.js:1455:11)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at TLSSocket.socketErrorListener (_http_client.js:401:9)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:66:8)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
I have already tried inserting the Forge API inside my react js project, and figure it out it would be a CORS problems.
const snapshot = change.after;
console.log(snapshot)
const api = "https://developer.api.autodesk.com/authentication/v1/authenticate"
const search = () =>
fetch(`${api}`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: JSON.stringify(`client_id=${process.env.REACT_APP_FORGE_CLIENT_ID}&client_secret=${process.env.REACT_APP_FORGE_CLIENT_SECRET}&grant_type=client_credentials&scope=data:read`)
}).then(res => res.json())
search().then((res) => {
const data = res
return snapshot.ref.parent.child('token').set(data);
})
})
Since Firebase Functions runs in the GCD backend CORS does not really come into play ...
You must be on the free plan - getaddrinfo EAI_AGAIN indicates a DNS lookup timeout and is due to the limitations of the free tier where outbound networking is limited to within Google services. Upgrade your plan to Flame or Blaze.

How to add files in a POST using request-promise of npm?

I'm tryng to send a Curl request using "request-promise" of npm. The Curl that I want to send is as follows:
`curl \
-H "Content-Type: multipart/form-data" \
-F "original=#./${parent_path}" \
-F "modified=#./${version_path}" \
-o "${out_path}" \
${URI}`
My code in node is:
BIMFile.findOne({ _id: responseDB.parent_id })
.then(parent => {
parent_path = parsePath(parent.path);
version_path = parsePath(responseDB.path);
console.log("PARENT!", parent_path, version_path);
const URI =
`${protocol}://${host_img_diff}:${port_img_diff}/diff`
out_path = version_path + '.tmp.jpg';
request.post({
url: URI,
formData: {
file: fs.createReadStream(parent_path),
file: fs.createReadStream(version_path)
}
}).then((apiResponse) => {
console.log('apiUPDATEResponse', apiResponse);
})
The result is:
Unhandled rejection StatusCodeError: 400 - "<!DOCTYPE HTML PUBLIC
\"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>400 Bad
Request</title>\n<h1>Bad Request</h1>\n<p>The browser (or proxy) sent a re
quest that this server could not understand.</p>\n"
at new StatusCodeError (/backend/node_modules/request-promise-
core/lib/errors.js:32:15)
at Request.plumbing.callback (/backend/node_modules/request-promise-
core/lib/plumbing.js:104:33)
at Request.RP$callback [as _callback] (/backend/node_modules/request-
promise-core/lib/plumbing.js:46:31)
at Request.self.callback (/backend/node_modules/request/request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/backend/node_modules/request/request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous>
(/backend/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1055:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
The server returns the following message:
xxx.xx.x.xx - - [05/Apr/2019 10:00:15] "POST /diff HTTP/1.1" 400 -
As you can see the server couldn't understand the post request. Does anyone know how to add files correctly?
Use send files using formData as you're doing, but one of the issues you have is that you're setting both files in the same property, and only the last one is set.
console.log({ file: 1, file: 2 });
So if file can receive multiple files, you need to use an array
const formData = {
file: [
fs.createReadStream(parent_path),
fs.createReadStream(version_path)
]
}
If you need additional meta-data, request module provides a way too:
Pass optional meta-data with an 'options' object with style:
{value: DATA, options: OPTIONS}
Use case: for some types of
streams, you'll need to provide "file"-related information manually.
See the form-data README for more information about options:
https://github.com/form-data/form-data

How to make AWS S3 request retryable?

Sometimes when I download multiple files from S3 bucket using node sdk, the request will timeout for one of the downloads. I would like for the request to just retry to attempt to download again.
The json error response says retryable: false.
Is there a way I can configure it to be true?
Here is the error:
{ TimeoutError: Connection timed out after 480000ms
at ClientRequest.<anonymous> (node_modules/aws-sdk/lib/http/node.js:83:34)
at Object.onceWrapper (events.js:272:13)
at ClientRequest.emit (events.js:180:13)
at ClientRequest.emit (domain.js:421:20)
at TLSSocket.emitTimeout (_http_client.js:703:34)
at Object.onceWrapper (events.js:272:13)
at TLSSocket.emit (events.js:180:13)
at TLSSocket.emit (domain.js:421:20)
at TLSSocket.Socket._onTimeout (net.js:396:8)
at ontimeout (timers.js:466:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:267:5)
message: 'Connection timed out after 480000ms',
code: 'TimeoutError',
time: 2019-04-01T17:58:41.010Z,
region: 'us-west-2',
hostname: 'bucket-name',
retryable: false,
statusCode: 200,
retryDelay: 129.76727762396757 }
I am not sure of how you are using the resources as you didn't share your code, but this might help you.
// setting retries
var s3 = new AWS.S3({apiVersion: '2006-03-01', maxRetries:10});
S3 sdk documentation - maxRetries

Uncaught NetworkingError: Cannot read property 'replace' of undefined Error

I'm getting the following error when trying to run the following code. My goal is to delete all items in both the User and Item tables. User and Item correspond to Dynamoose models.
The for loop is running correctly. But the scan().exec callback function isn't being run at all and it's throwing the following error.
What is really strange is on CircleCI this problem doesn't happen. It only happens on my local computer. I have tried removing node_modules and running npm install with no luck. Also it works fine if I remove the code below.
Any ideas? Or even where to start debugging this?
Code:
var dbarray = [User, Item];
for (var i = 0; i < dbarray.length; i++) {
dbarray[i].scan().exec(function(err, items) {
if (err) {
throw err;
}
items.forEach(function(item, key) {
item.delete();
});
});
}
Error:
General
1) "before each" hook
0 passing (10s)
1 failing
1) "before each" hook:
Uncaught NetworkingError: Cannot read property 'replace' of undefined
at findTargetPort (node_modules/zombie/lib/reroute.js:50:28)
at Socket.Net.Socket.connect (node_modules/zombie/lib/reroute.js:69:18)
at Agent.connect [as createConnection] (net.js:106:35)
at Agent.createSocket (_http_agent.js:217:26)
at Agent.addRequest (_http_agent.js:187:10)
at new ClientRequest (_http_client.js:272:16)
at Object.request (http.js:39:10)
at features.constructor.handleRequest (node_modules/aws-sdk/lib/http/node.js:42:23)
at executeSend (node_modules/aws-sdk/lib/event_listeners.js:304:29)
at Request.SEND (node_modules/aws-sdk/lib/event_listeners.js:318:9)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:101:18)
at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:115:18)
at callNextListener (node_modules/aws-sdk/lib/sequential_executor.js:95:12)
at node_modules/aws-sdk/lib/event_listeners.js:220:9
at finish (node_modules/aws-sdk/lib/config.js:315:7)
at node_modules/aws-sdk/lib/config.js:333:9
at Credentials.get (node_modules/aws-sdk/lib/credentials.js:126:7)
at getAsyncCredentials (node_modules/aws-sdk/lib/config.js:327:24)
at Config.getCredentials (node_modules/aws-sdk/lib/config.js:347:9)
at Request.SIGN (node_modules/aws-sdk/lib/event_listeners.js:192:22)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:101:18)
at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:115:18)
at Timeout.callNextListener [as _onTimeout] (node_modules/aws-sdk/lib/sequential_executor.js:95:12)
The issue turned out to be with Zombie.js. Specially Browser.localhost('localhost', port);. Removing that line and passing in http://localhost:3000 before each visit command solved the problem.
Not sure why the error was mentioning AWS and things like that.

New to JSON in nodejs getting some errors

getJSON('https://api.twitch.tv/kraken/streams/Jonathan_x64',
function(channel) {
if (channel["stream"] == null) {
//do something
} else {
////do something else
}
});
that is my current code but when i run it i get the following error
if (channel["stream"] == null) {
^
TypeError: Cannot read property 'stream' of undefined
at E:\my ultemet bot\index.js:10:16
at Request._callback (E:\my ultemet bot\node_modules\get-JSON\lib\node.js:11:5)
at Request.self.callback (E:\my ultemet bot\node_modules\request\request.js:200:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (E:\my ultemet bot\node_modules\request\request.js:1067:10)
at emitOne (events.js:101:20)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (E:\my ultemet bot\node_modules\request\request.js:988:12)
at emitNone (events.js:91:20)
As far as I know there isn't a built-in top level getJSON() function in Node so you must be using a custom function.
From the stack trace you've shared:
at Request._callback (E:\my ultemet bot\node_modules\get-JSON\lib\node.js:11:5)
^^^^^^^^^^^^^^^^^^^^^
... we learn that you are using an NPM third-party module. Once there, it's trivial to find the documentation:
var getJSON = require('get-json')
getJSON('http://api.listenparadise.org', function(error, response){
error
// undefined
response.result
// ["Beth Orton — Stolen Car",
// "Jack White — Temporary Ground",
// "I Am Kloot — Loch",
// "Portishead — Glory Box"]
response.ok
// => true
})
It isn't real code but it's clear that the first callback argument is error, but you have this:
function(channel){}
Since (as the error message states) it's undefined, that means that the call is successful—it's just you aren't reading it correctly.
I've been peeking the module source code and it's actually not very impressive. It's basically a tiny wrapper for request that doesn't add much value.

Categories